当前位置:网站首页>LeetCode每日两题02:反转字符串中的单词 (均1200道)
LeetCode每日两题02:反转字符串中的单词 (均1200道)
2022-08-10 21:51:00 【那人独钓寒江雪.】
题目如下:

解题思路:运用两个StringBuilder,一个用来承载反转字符串 一个是用来记录反转单词
使用字符确定c包含空格的时候 则向第一个承载反转字符串内添加反转单词的数据并且添加空格,然后new一个新的添加反转字符串的函数。最后return返回结果
class Solution {
public String reverseWords(String s) {
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
int len = s.length();//减少消耗的内存
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 (text printing tool awk)
华为路由器旁挂引流实验(使用流策略)
华为HCIE云计算之Fusion Access桌面云
《DevOps围炉夜话》- Pilot - CNCF开源DevOps项目DevStream简介 - feat. PMC成员胡涛
字节跳动原来这么容易就能进去...
LeetCode-402-移掉K位数字
美味石井饭菜
带着昇腾去旅行:一日看尽金陵城里的AI胜景
Using SylixOS virtual serial port, serial port free implementation system
LeetCode-402 - Remove K digits
[Maui official version] Create a cross-platform Maui program, as well as the implementation and demonstration of dependency injection and MVVM two-way binding
Regular expression of shell programming and text processor
接口测试的概念、目的、流程、测试方法有哪些?
What is Jmeter? What are the principle steps used by Jmeter?
如何保护 LDAP 目录服务中的用户安全?
QT笔记——vs + qt 创建一个带界面的 dll 和 调用带界面的dll
论文解读(g-U-Nets)《Graph U-Nets》
Live Classroom System 09--Tencent Cloud VOD Management Module (1)
[SQL brush questions] Day3----Special exercises for common functions that SQL must know
FPGA - Memory Resources of 7 Series FPGA Internal Structure -03- Built-in Error Correction Function









