当前位置:网站首页>[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
}
}
边栏推荐
- 近视:摘镜or配镜?这些问题必须先了解清楚
- Is it safe to open an account with flush?
- Unreal_ Datatable implements ID self increment and sets rowname
- Colab works with Google cloud disk
- TCP擁塞控制詳解 | 3. 設計空間
- 深入理解 SQL 中的 Grouping Sets 语句
- Aike AI frontier promotion (7.3)
- 8 tips for effective performance evaluation
- 在ntpdate同步时间的时候出现“the NTP socket is in use, exiting”
- The mixlab editing team is recruiting teammates~~
猜你喜欢

Rk3399 platform development series explanation (WiFi) 5.54. What is WiFi wireless LAN

Netease UI automation test exploration: airtest+poco

Visual SLAM algorithms: a survey from 2010 to 2016

Remote file contains actual operation

A survey of state of the art on visual slam

There are several APIs of airtest and poco that are easy to use wrong in "super". See if you have encountered them

8 cool visual charts to quickly write the visual analysis report that the boss likes to see

NSQ source code installation and operation process

arduino-esp32:LVGL项目(一)整体框架

NSQ源码安装运行过程
随机推荐
Learn from me about the enterprise flutter project: simplified framework demo reference
Develop team OKR in the way of "crowdfunding"
用同花顺炒股开户安全吗?
斑馬識別成狗,AI犯錯的原因被斯坦福找到了
The mixlab editing team is recruiting teammates~~
[combinatorics] combinatorial identities (review of eight combinatorial identities | product of combinatorial identities 1 | proof | use scenario | general method for finding combinatorial numbers)
特征多项式与常系数齐次线性递推
1287. Elements that appear more than 25% in an ordered array
Why does the std:: string operation perform poorly- Why do std::string operations perform poorly?
[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
Colab works with Google cloud disk
Eleven requirements for test management post
Register in PHP_ Globals parameter settings
跟我学企业级flutter项目:简化框架demo参考
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
Netease UI automation test exploration: airtest+poco
PHP CI(CodeIgniter)log级别设置
Hong Kong Polytechnic University | data efficient reinforcement learning and adaptive optimal perimeter control of network traffic dynamics
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
Leetcode binary search tree