当前位置:网站首页>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();//返回结果
}
}

边栏推荐
猜你喜欢

虚拟地址空间

What are the concepts, purposes, processes, and testing methods of interface testing?

谁是边缘计算服务的采购者?是这六个关键角色

【PCBA scheme design】Bluetooth skipping scheme

Black cats take you learn Makefile article 13: a Makefile collection compile problem

从斐波那契 - 谈及动态规划 - 优化

Thread State 详解

学会开会|成为有连接感组织的重要技能

如何成为一名正义黑客?你应该学习什么?

交换机和生成树知识点
随机推荐
LeetCode-498 - Diagonal Traversal
TCP连接过程中如果拔掉网线会发生什么?
Live Classroom System 08 Supplement - Tencent Cloud Object Storage and Course Classification Management
交换机和生成树知识点
labelme - block drag and drop events
These must-know JVM knowledge, I have sorted it out with a mind map
ThreadLocal comprehensive analysis (1)
xshell (sed command)
JVM classic fifty questions, now the interview is stable
Merge k sorted linked lists
shell脚本循环语句for、while语句
unusual understanding
测试4年感觉和1、2年时没什么不同?这和应届生有什么区别?
黑猫带你学Makefile第12篇:常见Makefile问题汇总
基于交流潮流的电力系统多元件N-k故障模型研究(Matlab代码实现)【电力系统故障】
Exploration and practice of the "zero trust" protection and data security governance system of the ransomware virus of Meichuang Technology
xshell (sed 命令)
win系统下pytorch深度学习环境安装
[SQL brush questions] Day3----Special exercises for common functions that SQL must know
亲测有效|处理风控数据特征缺失的一种方法