当前位置:网站首页>[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
}
}
边栏推荐
- 跟我学企业级flutter项目:简化框架demo参考
- Custom plug-in construction and use of QT plug-in
- Golang 装饰器模式以及在NSQ中的使用
- 8 cool visual charts to quickly write the visual analysis report that the boss likes to see
- Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
- What is the maximum number of concurrent TCP connections for a server? 65535?
- Is it safe to open an account with tongdaxin?
- [web security] - [SQL injection] - error detection injection
- Explore Netease's large-scale automated testing solutions see here see here
- Golang anonymous function use
猜你喜欢
Visual SLAM algorithms: a survey from 2010 to 2016
How to use AAB to APK and APK to AAB of Google play apps on the shelves
Colab works with Google cloud disk
Deep understanding of grouping sets statements in SQL
One article takes you to understand machine learning
Google Earth engine (GEE) - daymet v4: daily surface weather data set (1000m resolution) including data acquisition methods for each day
一台服务器最大并发 tcp 连接数多少?65535?
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (4)
To resist 7-Zip, list "three sins"? Netizen: "is the third key?"
随机推荐
Characteristic polynomial and constant coefficient homogeneous linear recurrence
Svn usage specification
特征多项式与常系数齐次线性递推
Batch files: list all files in a directory with relative paths - batch files: list all files in a directory with relative paths
Netease UI automation test exploration: airtest+poco
面试官:JVM如何分配和回收堆外内存
Multithread 02 thread join
AcWing 第58 场周赛
Extraction of the same pointcut
Qt插件之自定义插件构建和使用
Unity项目优化案例一
利用MySQL中的乐观锁和悲观锁实现分布式锁
Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
[combinatorics] combinatorial identities (review of eight combinatorial identities | product of combinatorial identities 1 | proof | use scenario | general method for finding combinatorial numbers)
Leetcode binary search tree
【LeetCode】94. Middle order traversal of binary tree
First knowledge of database
Myopia: take off or match glasses? These problems must be understood clearly first
Eleven requirements for test management post
Learn from me about the enterprise flutter project: simplified framework demo reference