当前位置:网站首页>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 )
边栏推荐
- Common skills of quantitative investment -- Drawing Part 1: Drawing stock closing price curve and ochl candle chart
- Pysmb usage
- [JS component] calendar
- Common skills for quantitative investment - indicators Chapter 3: detailed explanation of RSI indicators, their code implementation and drawing
- [Latex] 插入圖片
- Androi天气
- Kotlin coroutine withcontext switch thread
- Leetcode-11- container with the most water (medium)
- MySQL performance analysis - explain
- 今日睡眠质量记录74分
猜你喜欢
Unity calls alertdialog
What is pytorch? Explain the basic concepts of pytorch
Deadlock problem summary
Binary tree - right view
Several categories of software testing are clear at a glance
Most elements leetcode
How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of strategic benefits of ASI, VR, arbr, DPO and trix indicators
Mathematical knowledge arrangement: extremum & maximum, stagnation point, Lagrange multiplier
Minimum spanning tree problem
Opencv desaturation
随机推荐
How many steps are appropriate for each cycle of deep learning?
Characteristics of transactions -- atomicity (implementation principle)
[Latex] 插入圖片
Wal mechanism of MySQL
Get preview of precast body
Androi天气
生态聚合NFT来袭,Metaverse Ape引领Web 3.0元宇宙新范式革命
Android Weather
[JS component] create a custom horizontal and vertical scroll bar following the steam style
AOF持久化
Leetcode-13- Roman numeral to integer (simple)
Canvas airplane game
With a market value of more than trillion yuan and a sales volume of more than 100000 yuan for three consecutive months, will BYD become the strongest domestic brand?
Cards are unpredictable
Self use notes for problem brushing learning
spiral matrix visit Search a 2D Matrix
How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of DBCD, ROC, vroc, Cr and psy index strategy income
Sequence table - find main element
. The way to prove the effect of throwing exceptions on performance in. Net core
How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of strategic benefits of ASI, VR, arbr, DPO and trix indicators