当前位置:网站首页>16、字符串反转
16、字符串反转
2022-07-28 09:58:00 【[email protected]】
题目描述
- 输入: " hello world! "
输出: “world! hello”
解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。 - 输入: “a good example”
输出: “example good a”
解释: 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。
public String reverseWords(String s) {
String[] strs = s.trim().split(" ");// 删除首尾空格,分割字符串
StringBuilder res = new StringBuilder();
for(int i = strs.length - 1; i >= 0; i--){// 倒序遍历单词列表
if(strs[i].equals(" ")) continue;// 遇到空单词则跳过
res.append(strs[i] + " ");// 将单词拼接至 StringBuilder
}
return res.toString().trim();// 转化为字符串,删除尾部空格,并返回
}
总体思路:将字符串存入数组,然后从数组尾端进行遍历,但是在遍历的时候,会遇到两个空格的时候,这时候就要忽略掉,继续遍历,同时将遍历的每个字符存入StringBuilder,最后转为字符串输出。
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://blog.csdn.net/zjj1910066023/article/details/124910650
边栏推荐
- ️雄关漫道真如铁,而今迈步从头越️
- PHP生成二维码(学习)
- Data can't lie. Plato farm is the leader of the meta universe
- Massive data topn problem
- Introduction to evaluatorfilter
- Kubernetes
- 13 probability distributions that must be understood in deep learning
- centos7下安装mysql,网上文章都不太准
- Introduction to timebasedrollingpolicy
- [learning notes] border and period
猜你喜欢

语音聊天app——如何规范开发流程?
![[esp32][esp idf][lvgl7.9] failed to compile with OLED IIC](/img/16/e5aa43df6ef9bdbc173fa547c85559.png)
[esp32][esp idf][lvgl7.9] failed to compile with OLED IIC

二分、三分、01分数规划【第III弹】
![[ESP32][esp-idf] AP+STA实现无线桥接 中转wifi信号](/img/bf/0a968064a8f7c11b86a2a2820208e6.png)
[ESP32][esp-idf] AP+STA实现无线桥接 中转wifi信号

选择供应商服务系统,是大健康产业企业迈向数字化转型的第一步

Voice chat app - how to standardize the development process?
![[jzof] 14 cut rope](/img/36/6f58b443a549ad245c1c4cfe5d13af.png)
[jzof] 14 cut rope

arthas使用教程

On July 13, 2021, we collapsed like this

【JS高级】js之函数、重载、匿名函数、作用域及作用域链_03
随机推荐
Redis设计规范
Cloudcompare & PCL matching point sampling consistency suppression
Redis interview questions must be known and learned
OSPF的拓展配置,选路原则,防环及重发布
Thinking and summary of technical personnel | R & D Efficiency
Joint search set
Massive data topn problem
语音聊天app——如何规范开发流程?
Oracle中的小知识
Leetcode076 -- the kth largest number in the array
Etcd(高可用KV数据库)
In retaliation for the dismissal of the company, I changed all code comments of the project!
Boss: there are too many systems in the company. Can we realize account interworking?
[esp32][esp idf] ap+sta realizes wireless bridging and transferring WiFi signals
select如果不加order by子句,返回结果的顺序是不可靠的
LinkedList源码按摩,啊舒服
OSPF的LSA及优化
Flink - checkpoint Failure reason: Not all required tasks are currently running
Kubernetes
In hot weather, the line of defense for safe production was strengthened, and Guangzhou Haizhu District carried out emergency drills for gas stations