当前位置:网站首页>[sword finger offer] 58 - I. flip the word order
[sword finger offer] 58 - I. flip the word order
2022-07-03 16:32:00 【LuZhouShiLi】
The finger of the sword Offer 58 - I. Flip word order
subject
Enter an English sentence , Turn over the order of the words in the sentence , But the order of the characters in the word is the same . For the sake of simplicity , Punctuation is treated like ordinary letters . For example, input string "I am a student. “, The output "student. a am I”.
Ideas
Double pointer , Flashback traversal string s, Record the left and right index boundaries of words i,j. Determine the boundary of one word at a time , Just add it to the word list res in , Final , Splice the word list into a string , And return .
Code
class Solution {
public String reverseWords(String s) {
s = s.trim();// Delete leading and trailing spaces
int j = s.length() - 1,i = j;
StringBuilder res = new StringBuilder();
// Search back and forth
// "hello world"
while(i >= 0)
{
while(i >= 0 && s.charAt(i) != ' ') i--;// Search for the first space Space encountered i stop searching
// Split string from i + 1 Start is not a space And then to j Location (j+1 Can't get )
res.append(s.substring(i + 1,j + 1) + " ");// Add words
while(i >= 0 && s.charAt(i) == ' ')
{
i--;// Skip spaces between words
}
// to update j Then move on to the next step i
j = i;
}
return res.toString().trim();// Convert to string and return
}
}
边栏推荐
- 什么是质押池,如何进行质押呢?
- 【LeetCode】94. Middle order traversal of binary tree
- [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
- Pychart error updating package list: connect timed out
- Visual SLAM algorithms: a survey from 2010 to 2016
- One article takes you to understand machine learning
- 0214-27100 a day with little fluctuation
- Golang 装饰器模式以及在NSQ中的使用
- 面试官:JVM如何分配和回收堆外内存
- Leetcode binary search tree
猜你喜欢
[proteus simulation] 74hc595+74ls154 drive display 16x16 dot matrix
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
NSQ源码安装运行过程
斑馬識別成狗,AI犯錯的原因被斯坦福找到了
MySQL single table field duplicate data takes the latest SQL statement
2022爱分析· 国央企数字化厂商全景报告
Cocos Creator 2. X automatic packaging (build + compile)
0214-27100 a day with little fluctuation
TCP拥塞控制详解 | 3. 设计空间
PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
随机推荐
How programming apes grow rapidly
Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
MongoDB 的安装和基本操作
架构实战营 - 第 6 期 毕业总结
关于视觉SLAM的最先进技术的调查-A survey of state-of-the-art on visual SLAM
【LeetCode】94. Middle order traversal of binary tree
Svn usage specification
Pychart error updating package list: connect timed out
Colab works with Google cloud disk
Leetcode binary search tree
"Everyday Mathematics" serial 56: February 25
消息队列消息丢失和消息重复发送的处理策略
PHP中register_globals参数设置
Mongodb installation and basic operation
TCP拥塞控制详解 | 3. 设计空间
斑马识别成狗,AI犯错的原因被斯坦福找到了
Why does the std:: string operation perform poorly- Why do std::string operations perform poorly?
Mysql 单表字段重复数据取最新一条sql语句
Expression of request header in different countries and languages
面试之 top k问题