当前位置:网站首页>[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
}
}
边栏推荐
- arduino-esp32:LVGL项目(一)整体框架
- MySQL single table field duplicate data takes the latest SQL statement
- [combinatorics] non descending path problem (outline of non descending path problem | basic model of non descending path problem | non descending path problem expansion model 1 non origin starting poi
- NSQ源码安装运行过程
- Expression of request header in different countries and languages
- 如何在本机搭建SVN服务器
- "The NTP socket is in use, exiting" appears when ntpdate synchronizes the time
- 用通达信炒股开户安全吗?
- 于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
- Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (I)
猜你喜欢
Unreal_ Datatable implements ID self increment and sets rowname
0214-27100 a day with little fluctuation
近视:摘镜or配镜?这些问题必须先了解清楚
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
[combinatorics] non descending path problem (outline of non descending path problem | basic model of non descending path problem | non descending path problem expansion model 1 non origin starting poi
Processing strategy of message queue message loss and repeated message sending
2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
面试官:JVM如何分配和回收堆外内存
TCP congestion control details | 3 design space
Data driving of appium framework for mobile terminal automated testing
随机推荐
Zebras are recognized as dogs, and Stanford found the reason why AI made mistakes
Data driving of appium framework for mobile terminal automated testing
手机注册股票开户安全吗 开户需要钱吗
[combinatorics] summary of combinatorial identities (eleven combinatorial identities | proof methods of combinatorial identities | summation methods)*
Unreal_DataTable 实现Id自增与设置RowName
"The NTP socket is in use, exiting" appears when ntpdate synchronizes the time
Pointcut expression
Nifi from introduction to practice (nanny level tutorial) - flow
特征多项式与常系数齐次线性递推
How to initialize views when loading through storyboards- How is view initialized when loaded via a storyboard?
Mysql 将逗号隔开的属性字段数据由列转行
[proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display
[solved] access denied for user 'root' @ 'localhost' (using password: yes)
Mixlab编辑团队招募队友啦~~
Advanced Mathematics (Seventh Edition) Tongji University exercises 2-1 personal solutions
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (I)
《天天数学》连载56:二月二十五日
Mysql 单表字段重复数据取最新一条sql语句
MongoDB 的安装和基本操作
PHP中register_globals参数设置