当前位置:网站首页>leetcode. 151. flip the words in the string
leetcode. 151. flip the words in the string
2022-06-13 01:03:00 【Didi dada】
- Flip the words in the string
Give you a string s , Flip all in the string one by one word .
A word is a string of non whitespace characters .s Use in At least A space in the string Word separation .
Please return to a flip s Chinese word order and use Single space Connected strings .
explain :
Input string s It can be in the front 、 Contain extra spaces after or between words .
After turning over, the words should be Separate with only one space .
In the flipped string Should not contain extra spaces .
Example 1:
Input :s = “the sky is blue”
Output :“blue is sky the”
Example 2:
Input :s = " hello world "
Output :“world hello”
explain : The input string can contain extra spaces before or after , However, the flipped characters cannot include .
Example 3:
Input :s = “a good example”
Output :“example good a”
explain : If there are extra spaces between two words , Reduce the space between words after flipping to only contain one .
Example 4:
Input :s = " Bob Loves Alice "
Output :“Alice Loves Bob”
Example 5:
Input :s = “Alice does not even like bob”
Output :“bob like even not does Alice”
- C++
class Solution {
public:
string reverseWords(string s) {
vector<string> tmp;
int n = s.size();
string word;
for(int i=0;i<n;i++){
if(s[i]==' '){
if(word.size()!=0){
tmp.emplace_back(word);
word = "";
}
}
else
word+=s[i];
}
if(word.size())
tmp.emplace_back(word);
reverse(tmp.begin(),tmp.end());
string res;
for(auto it=tmp.begin();it!=tmp.end();it++){
res+=*it;
res+=' ';
}
res.pop_back();
return res;
}
};
- Note that there are spaces in the beginning and two consecutive spaces .
- python
class Solution:
def reverseWords(self, s: str) -> str:
s = s.strip()
sequence = s.split(' ')
sequence.reverse()
res = ''
for it in sequence:
if it!='':
res+=it
res+=' '
return res[:-1]
- Pay attention to the case of continuous spaces ( When consecutive spaces exist split() The function will leave some spaces )
边栏推荐
- The seventh finals of the Blue Bridge Cup
- Kotlin coroutine suspend function suspend keyword
- Go simple read database
- Breadth first search for node editor runtime traversal
- [Latex] 插入圖片
- Most elements leetcode
- Unitywebrequest asynchronous Download
- Canvas game lower level 100
- How the ET framework uses it to develop games
- Androi weather
猜你喜欢
![[JS component] customize the right-click menu](/img/a3/4555619db17e4c398e72c7d6b12f5d.jpg)
[JS component] customize the right-click menu

Alexnet实现Caltech101数据集图像分类(pytorch实现)

Common skills for quantitative investment - indicators Chapter 3: detailed explanation of RSI indicators, their code implementation and drawing

Minimum spanning tree problem
![[latex] insérer une image](/img/0b/3304aaa03d3fea3ebb93b0348c3131.png)
[latex] insérer une image

What is dummy change?

Cards are unpredictable

What kind of experience is it to be a software test engineer in a state-owned enterprise: every day is like a war

What is pytorch? Explain the basic concepts of pytorch
![[JS component] floating text](/img/e5/7faad5422bba919bed34e3dbcf7ba0.jpg)
[JS component] floating text
随机推荐
5G工业网关在煤矿行业的应用优势
Ecological convergence NFT attacks, metaverse ape leads the new paradigm revolution of Web 3.0 meta universe
Common skills for quantitative investment - indicators Chapter 3: detailed explanation of RSI indicators, their code implementation and drawing
[Latex] 插入图片
[latex] insert picture
[backtrader source code analysis 7] analysis of the functions for calculating mean value, variance and standard deviation in mathsupport in backtrader (with low gold content)
Unity extension
[North Asia server data recovery] data recovery case of Hyper-V service paralysis caused by virtual machine file loss
MySQL transaction
Matrix fast power
Mathematical knowledge arrangement: extremum & maximum, stagnation point, Lagrange multiplier
Androi weather
Rest at home today
Undirected graph -- computing the degree of a node in compressed storage
Why is there always a space (63 or 2048 sectors) in front of the first partition when partitioning a disk
(01).NET MAUI实战 建项目
Androi天氣
[server data recovery] successful cases of data loss recovery during data migration between storage servers
Expression tree - medium order printout
What is meebits? A brief explanation