当前位置:网站首页>[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
}
}
边栏推荐
- Expression of request header in different countries and languages
- Le zèbre a été identifié comme un chien, et la cause de l'erreur d'AI a été trouvée par Stanford
- Multithread 02 thread join
- 【剑指 Offer】58 - II. 左旋转字符串
- PHP二级域名session共享方案
- EditText request focus - EditText request focus
- Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (III)
- Basis of target detection (IOU)
- 中南大学|通过探索理解: 发现具有深度强化学习的可解释特征
- Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (4)
猜你喜欢
Threejs Part 2: vertex concept, geometry structure
关于视觉SLAM的最先进技术的调查-A survey of state-of-the-art on visual SLAM
arduino-esp32:LVGL项目(一)整体框架
Explore Netease's large-scale automated testing solutions see here see here
Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
The mixlab editing team is recruiting teammates~~
Visual SLAM algorithms: a survey from 2010 to 2016
Initial test of scikit learn Library
Mixlab编辑团队招募队友啦~~
TCP congestion control details | 3 design space
随机推荐
中南大学|通过探索理解: 发现具有深度强化学习的可解释特征
QT串口ui设计和解决显示中文乱码
Advanced Mathematics (Seventh Edition) Tongji University exercises 2-1 personal solutions
Unity项目优化案例一
Aike AI frontier promotion (7.3)
NSQ源码安装运行过程
Construction practice camp - graduation summary of phase 6
深入理解 SQL 中的 Grouping Sets 语句
Extraction of the same pointcut
Pychart error updating package list: connect timed out
Using optimistic lock and pessimistic lock in MySQL to realize distributed lock
Unreal_ Datatable implements ID self increment and sets rowname
Data driving of appium framework for mobile terminal automated testing
Threejs Part 2: vertex concept, geometry structure
Register in PHP_ Globals parameter settings
Caching mechanism of Hibernate / session level caching mechanism
Explore Netease's large-scale automated testing solutions see here see here
用通达信炒股开户安全吗?
[combinatorics] summary of combinatorial identities (eleven combinatorial identities | proof methods of combinatorial identities | summation methods)*
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (I)