当前位置:网站首页>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();//返回结果
}
}
边栏推荐
猜你喜欢
shell脚本循环语句for、while语句
These must-know JVM knowledge, I have sorted it out with a mind map
罗克韦尔AB PLC RSLogix5000中计数器指令使用方法介绍
virtual address space
【开源教程5】疯壳·开源编队无人机-飞控固件烧写
财务年报怎样翻译,为什么要选择专业翻译公司?
GMT,UTC,CST,DST,RTC,NTP,SNTP,NITZ: 嵌入式的时间
geemap的详细安装步骤及环境配置
Shell programming specification and variables
云服务器基于 SSH 协议实现免密登录
随机推荐
Shell 编程--Sed
2022.8.8 Selected Lectures on Good Topics (Number Theory Field)
Service - DHCP principle and configuration
Power system power flow calculation (Newton-Raphson method, Gauss-Seidel method, fast decoupling method) (Matlab code implementation)
RTL8721DM 双频WIFI + 蓝牙5.0 物联网(IoT)应用
ASCII、Unicode和UTF-8
Labelme-5.0.1 version edit polygon crash
BM13判断一个链表是否为回文结构
罗克韦尔AB PLC RSLogix5000中计数器指令使用方法介绍
The Thread State,
Shell编程之条件语句(二)
Service - DNS forward and reverse domain name resolution service
财务年报怎样翻译,为什么要选择专业翻译公司?
高通平台开发系列讲解(应用篇)QCMAP应用框架介绍
virtual address space
合并k个已排序的链表
ThreadLocal全面解析(一)
一篇文章教你Pytest快速入门和基础讲解,一定要看
【640. 求解方程】
2022年8月的10篇论文推荐