当前位置:网站首页>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();//返回结果
}
}
边栏推荐
- 过滤器
- labelme-5.0.1版本编辑多边形闪退
- Intelligent scheme design - intelligent rope skipping scheme
- web逆向之丁香园
- 新一代网络安全防护体系的五个关键特征
- Shell programming specification and variables
- 元宇宙社交应用,靠什么吸引用户「为爱发电」?
- Service - DNS forward and reverse domain name resolution service
- QT笔记——vs + qt 创建一个带界面的 dll 和 调用带界面的dll
- 【开源教程5】疯壳·开源编队无人机-飞控固件烧写
猜你喜欢
【开源教程5】疯壳·开源编队无人机-飞控固件烧写
Service - DHCP principle and configuration
高通平台开发系列讲解(应用篇)QCMAP应用框架介绍
IM 即时通讯开发如何设计图片文件的服务端存储架构
接口测试的概念、目的、流程、测试方法有哪些?
Conditional Statements of Shell Programming (2)
What are the concepts, purposes, processes, and testing methods of interface testing?
LeetCode-498 - Diagonal Traversal
shell programming without interaction
Huawei router clock near the drainage experiment (using stream strategy)
随机推荐
2022.8.9 Mock Competition
Service - DNS forward and reverse domain name resolution service
unusual understanding
阿里云贾朝辉:云XR平台支持彼真科技呈现国风科幻虚拟演唱会
Extended Chinese Remainder Theorem
A shell script the for loop statements, while statement
shell programming without interaction
带着昇腾去旅行:一日看尽金陵城里的AI胜景
交换机和生成树知识点
LeetCode每日一题(1573. Number of Ways to Split a String)
Application of Spatial 3D Model Reconstruction Based on Pix4Dmapper - Spatial Analysis and Site Selection
IM 即时通讯开发如何设计图片文件的服务端存储架构
《DevOps围炉夜话》- Pilot - CNCF开源DevOps项目DevStream简介 - feat. PMC成员胡涛
camera preview process --- from HAL to OEM
美味石井饭菜
How to translate financial annual report, why choose a professional translation company?
shell脚本循环语句for、while语句
LeetCode-498-对角线遍历
RTL8721DM 双频WIFI + 蓝牙5.0 物联网(IoT)应用
uni-app微信小程序——下拉多选框