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

边栏推荐
- 今日睡眠质量记录75分
- How to translate financial annual report, why choose a professional translation company?
- 美味的佳肴
- labelme-屏蔽拖拽的事件
- 罗克韦尔AB PLC RSLogix5000中计数器指令使用方法介绍
- 2022.8.9 模拟赛
- Black cat takes you to learn Makefile Part 12: Summary of common Makefile problems
- 为什么一般公司面试结束后会说「回去等消息」,而不是直接告诉面试者结果?
- 交换机和生成树知识点
- 智能方案设计——智能跳绳方案
猜你喜欢

IM 即时通讯开发如何设计图片文件的服务端存储架构

阿里云张新涛:支持沉浸式体验应用快速落地,阿里云云XR平台发布

Using SylixOS virtual serial port, serial port free implementation system

直播课堂系统08补-腾讯云对象存储和课程分类管理

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

2022年8月的10篇论文推荐

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

一篇文章教你Pytest快速入门和基础讲解,一定要看

H3C S5130 IRF做堆叠

QT笔记——用VS + qt 生成dll 和 调用生成的dll
随机推荐
LeetCode每日一题(1573. Number of Ways to Split a String)
Translating scientific and technological papers, how to translate from Russian to Chinese
解码2022中国网安强星丨正向建、反向查,华为构建数字化时代的网络安全防线
labelme-5.0.1版本编辑多边形闪退
MySQL Advanced Commands
ASCII、Unicode和UTF-8
About DataFrame: Processing Time
商家招募电商主播要考虑哪些内容
如何保护 LDAP 目录服务中的用户安全?
A shell script the for loop statements, while statement
RTL8721DM 双频WIFI + 蓝牙5.0 物联网(IoT)应用
What are the concepts, purposes, processes, and testing methods of interface testing?
H3C S5130 IRF做堆叠
GMT,UTC,CST,DST,RTC,NTP,SNTP,NITZ: 嵌入式的时间
QT笔记——QT工具uic,rcc,moc,qmake的使用和介绍
MySQL高级指令
TCP连接过程中如果拔掉网线会发生什么?
直播课堂系统08-腾讯云对象存储和课程分类管理
Shell programming specification and variables
阿里云新增三大高性能计算解决方案,助力生命科学行业快速发展