当前位置:网站首页>[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
}
}
边栏推荐
- Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
- Learn from me about the enterprise flutter project: simplified framework demo reference
- 探索Cassandra的去中心化分布式架构
- Colab works with Google cloud disk
- Add color to the interface automation test framework and realize the enterprise wechat test report
- MySQL single table field duplicate data takes the latest SQL statement
- A survey of state of the art on visual slam
- MongoDB 的安装和基本操作
- Stm32f103c8t6 firmware library lighting
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
猜你喜欢

Visual SLAM algorithms: a survey from 2010 to 2016

为抵制 7-Zip,列出 “三宗罪” ?网友:“第3个才是重点吧?”

Record a jar package conflict resolution process

【LeetCode】94. Middle order traversal of binary tree

(Supplement) double pointer topic

What is the maximum number of concurrent TCP connections for a server? 65535?

深入理解 SQL 中的 Grouping Sets 语句

初试scikit-learn库

NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线

Explore Netease's large-scale automated testing solutions see here see here
随机推荐
Batch files: list all files in a directory with relative paths - batch files: list all files in a directory with relative paths
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
切入点表达式
PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
利用MySQL中的乐观锁和悲观锁实现分布式锁
Interviewer: how does the JVM allocate and recycle off heap memory
Deep understanding of grouping sets statements in SQL
[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
Client does not support authentication protocol requested by server; consider upgrading MySQL client
斑马识别成狗,AI犯错的原因被斯坦福找到了
Unity project optimization case 1
How programming apes grow rapidly
疫情常态化大背景下,关于远程办公的思考|社区征文
NSQ源码安装运行过程
[combinatorics] combinatorial identities (review of eight combinatorial identities | product of combinatorial identities 1 | proof | use scenario | general method for finding combinatorial numbers)
Golang anonymous function use
0214-27100 a day with little fluctuation
Characteristic polynomial and constant coefficient homogeneous linear recurrence
Chinese translation of Tagore's floating birds (1~10)