当前位置:网站首页>【剑指 Offer】58 - I. 翻转单词顺序
【剑指 Offer】58 - I. 翻转单词顺序
2022-07-03 16:29:00 【LuZhouShiLi】
剑指 Offer 58 - I. 翻转单词顺序
题目
输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。为简单起见,标点符号和普通字母一样处理。例如输入字符串"I am a student. “,则输出"student. a am I”。
思路
双指针,倒叙遍历字符串s,记录单词左右索引边界i,j。每次确定一个单词的边界,就将其添加到单词列表res中,最终,将单词列表拼接为字符串,并返回即可。
代码
class Solution {
public String reverseWords(String s) {
s = s.trim();// 删除首尾空格
int j = s.length() - 1,i = j;
StringBuilder res = new StringBuilder();
// 从后往前搜索
// "hello world"
while(i >= 0)
{
while(i >= 0 && s.charAt(i) != ' ') i--;// 搜索首个空格 遇到空格i停止搜索
// 分割字符串 从i + 1开始不是空格 然后到j位置(j+1取不到)
res.append(s.substring(i + 1,j + 1) + " ");// 添加单词
while(i >= 0 && s.charAt(i) == ' ')
{
i--;// 跳过单词间空格
}
// 更新j 然后下一步接着移动i
j = i;
}
return res.toString().trim();//转化为字符串并且返回
}
}
边栏推荐
- 爱可可AI前沿推介(7.3)
- arduino-esp32:LVGL项目(一)整体框架
- 消息队列消息丢失和消息重复发送的处理策略
- Qt插件之自定义插件构建和使用
- Mixlab编辑团队招募队友啦~~
- How programming apes grow rapidly
- 疫情常态化大背景下,关于远程办公的思考|社区征文
- Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)
- 面试之 top k问题
- Thinking about telecommuting under the background of normalization of epidemic | community essay solicitation
猜你喜欢

Détails du contrôle de la congestion TCP | 3. Espace de conception

拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。
![[redis foundation] understand redis persistence mechanism together (rdb+aof graphic explanation)](/img/68/3721975cf33fcfacc28dc4d3d6a5ca.jpg)
[redis foundation] understand redis persistence mechanism together (rdb+aof graphic explanation)

Visual SLAM algorithms: a survey from 2010 to 2016

Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs

【声明】关于检索SogK1997而找到诸多网页爬虫结果这件事

Unreal_DataTable 实现Id自增与设置RowName

NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon

How can technology managers quickly improve leadership?

Remote file contains actual operation
随机推荐
Caching mechanism of Hibernate / session level caching mechanism
Is it safe to open an account with flush?
Zebras are recognized as dogs, and Stanford found the reason why AI made mistakes
Chinese translation of Tagore's floating birds (1~10)
From the 18th line to the first line, the new story of the network security industry
Mixlab编辑团队招募队友啦~~
[proteus simulation] 74hc595+74ls154 drive display 16x16 dot matrix
Mongodb installation and basic operation
[combinatorics] combinatorial identities (sum of variable terms 3 combinatorial identities | sum of variable terms 4 combinatorial identities | binomial theorem + derivation to prove combinatorial ide
Colab works with Google cloud disk
QT串口ui设计和解决显示中文乱码
Effect of ARP package on FTP dump under vxworks-6.6 system
Cocos Creator 2. X automatic packaging (build + compile)
Hibernate的缓存机制/会话级缓存机制
架构实战营 - 第 6 期 毕业总结
Mysql 单表字段重复数据取最新一条sql语句
在ntpdate同步时间的时候出现“the NTP socket is in use, exiting”
近视:摘镜or配镜?这些问题必须先了解清楚
Initial test of scikit learn Library
2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises