当前位置:网站首页>【剑指 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();//转化为字符串并且返回
}
}
边栏推荐
- Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (III)
- One article takes you to understand machine learning
- 程序猿如何快速成长
- PHP CI(CodeIgniter)log级别设置
- 2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
- Effect of ARP package on FTP dump under vxworks-6.6 system
- [combinatorics] combinatorial identity (sum of combinatorial identity products 1 | sum of products 1 proof | sum of combinatorial identity products 2 | sum of products 2 proof)
- [statement] about searching sogk1997 and finding many web crawler results
- Add color to the interface automation test framework and realize the enterprise wechat test report
- Mb10m-asemi rectifier bridge mb10m
猜你喜欢

One article takes you to understand machine learning

Mysql 将逗号隔开的属性字段数据由列转行

NSQ source code installation and operation process

PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug

8 cool visual charts to quickly write the visual analysis report that the boss likes to see

2022爱分析· 国央企数字化厂商全景报告

Myopia: take off or match glasses? These problems must be understood clearly first

arduino-esp32:LVGL项目(一)整体框架

Record a jar package conflict resolution process

Remote file contains actual operation
随机推荐
8 cool visual charts to quickly write the visual analysis report that the boss likes to see
Netease UI automation test exploration: airtest+poco
PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
[combinatorics] combinatorial identity (sum of variable upper terms 1 combinatorial identity | summary of three combinatorial identity proof methods | proof of sum of variable upper terms 1 combinator
MongoDB 的安装和基本操作
Hong Kong Polytechnic University | data efficient reinforcement learning and adaptive optimal perimeter control of network traffic dynamics
Mixlab编辑团队招募队友啦~~
Le zèbre a été identifié comme un chien, et la cause de l'erreur d'AI a été trouvée par Stanford
Is it safe to open a stock account by mobile registration? Does it need money to open an account
Top k questions of interview
中南大学|通过探索理解: 发现具有深度强化学习的可解释特征
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
2022爱分析· 国央企数字化厂商全景报告
[list to map] collectors Tomap syntax sharing (case practice)
arduino-esp32:LVGL项目(一)整体框架
Aike AI frontier promotion (7.3)
[combinatorics] combinatorial identity (sum of combinatorial identity products 1 | sum of products 1 proof | sum of combinatorial identity products 2 | sum of products 2 proof)
1287. Elements that appear more than 25% in an ordered array
利用MySQL中的乐观锁和悲观锁实现分布式锁
Mb10m-asemi rectifier bridge mb10m