当前位置:网站首页>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();//返回结果
}
}
边栏推荐
- 阿里云贾朝辉:云XR平台支持彼真科技呈现国风科幻虚拟演唱会
- These must-know JVM knowledge, I have sorted it out with a mind map
- 配电网络扩展规划:考虑使用概率性能源生产和消费概况的决策(Matlab代码实现)
- labelme - block drag and drop events
- How to translate financial annual report, why choose a professional translation company?
- QT笔记——用VS + qt 生成dll 和 调用生成的dll
- GMT,UTC,CST,DST,RTC,NTP,SNTP,NITZ: 嵌入式的时间
- Live Classroom System 08 Supplement - Tencent Cloud Object Storage and Course Classification Management
- uni-app微信小程序——下拉多选框
- ASCII、Unicode和UTF-8
猜你喜欢
随机推荐
STL-deque
Shell编程之条件语句(二)
链表相加(二)
Translating scientific and technological papers, how to translate from Russian to Chinese
字节跳动原来这么容易就能进去...
基于交流潮流的电力系统多元件N-k故障模型研究(Matlab代码实现)【电力系统故障】
mmpose关键点(一):评价指标(PCK,OKS,mAP)
Black cats take you learn Makefile article 13: a Makefile collection compile problem
ThreadLocal全面解析(一)
unusual understanding
Exploration and practice of the "zero trust" protection and data security governance system of the ransomware virus of Meichuang Technology
shell (text printing tool awk)
链表中的节点每k个一组翻转
Addition of linked lists (2)
虚拟地址空间
合并k个已排序的链表
shell编程之免交互
3D model reconstruction of UAV images based on motion structure restoration method based on Pix4Dmapper
geemap的详细安装步骤及环境配置
接口测试的概念、目的、流程、测试方法有哪些?