当前位置:网站首页>LeetCode Daily 2 Questions 02: Reverse the words in a string (1200 each)
LeetCode Daily 2 Questions 02: Reverse the words in a string (1200 each)
2022-08-10 22:34:00 【The man fishing alone like snow.】
题目如下:
解题思路:运用两个StringBuilder,One to carry the reversed string One is to record reverse words
Use characters to determinecwhen including spaces Then add the data of the reversed word to the first carrying the reversed string and add spaces,然后newA new function that adds reversed strings.最后return返回结果
class Solution {
public String reverseWords(String s) {
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
int len = s.length();//Reduce consumed memory
for (int i = 0; i < len; i++) {
char c = s.charAt(i);
if (c == ' ') {
sb1.append(sb2.reverse());
sb1.append(' ');
sb2 = new StringBuilder();
} else {
sb2.append(c);
}
}
sb1.append(sb2.reverse());
return sb1.toString();//返回结果
}
}
边栏推荐
- 商家招募电商主播要考虑哪些内容
- 使用 Cloudreve 搭建私有云盘
- Translating scientific and technological papers, how to translate from Russian to Chinese
- 爬虫request.get()出现错误
- What are the concepts, purposes, processes, and testing methods of interface testing?
- LeetCode-402 - Remove K digits
- LeetCode每日一题(1573. Number of Ways to Split a String)
- BM13 determines whether a linked list is a palindrome
- STL-deque
- 交换机和生成树知识点
猜你喜欢
c语言之 练习题1 大贤者福尔:魔法数,神奇的等式
解码2022中国网安强星丨正向建、反向查,华为构建数字化时代的网络安全防线
《DevOps围炉夜话》- Pilot - CNCF开源DevOps项目DevStream简介 - feat. PMC成员胡涛
QT笔记——用VS + qt 生成dll 和 调用生成的dll
阿里云张新涛:支持沉浸式体验应用快速落地,阿里云云XR平台发布
阿里巴巴、蚂蚁集团推出分布式数据库 OceanBase 4.0,单机部署性能超 MySQL
C#【必备技能篇】Hex文件转bin文件的代码实现
LeetCode-402 - Remove K digits
JVM经典五十问,这下面试稳了
QT笔记——vs + qt 创建一个带界面的 dll 和 调用带界面的dll
随机推荐
谁是边缘计算服务的采购者?是这六个关键角色
黑猫带你学Makefile第12篇:常见Makefile问题汇总
LeetCode Daily Question (1573. Number of Ways to Split a String)
RADIUS Authentication Server Deployment Costs That Administrators Must Know
Play RT-THREAD of doxygen
美味的佳肴
2022年8月10日:使用 ASP.NET Core 为初学者构建 Web 应用程序--使用 ASP.NET Core 创建 Web UI(没看懂需要再看一遍)
链表相加(二)
STL-stack
APP UI自动化测试常见面试题,或许有用呢~
MySQL Advanced Commands
geemap的详细安装步骤及环境配置
pytorch手撕CNN
基于交流潮流的电力系统多元件N-k故障模型研究(Matlab代码实现)【电力系统故障】
过滤器
port forwarding
JS中使用正则表达式g模式和非g模式的区别
xshell (sed command)
Merge k sorted linked lists
黑猫带你学Makefile第11篇:当头文件a.h改变时,如何将所有依赖头文件a.h的.c文件都重新编译