当前位置:网站首页>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 )
边栏推荐
- Get preview of precast body
- [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)
- redis
- 软件测试的几种分类,一看就明了
- Quick power explanation
- 深度学习模型剪枝
- Et5.0 value type generation
- Traditional machine learning classification model predicts the rise and fall of stock prices under more than 40 indicators
- Characteristics of transactions - persistence (implementation principle)
- ROS从入门到精通(零) 教程导读
猜你喜欢

Common skills of quantitative investment - index part 2: detailed explanation of BOL (Bollinger line) index, its code implementation and drawing

軟件測試的幾種分類,一看就明了

ArrayList underlying source code

Common skills of quantitative investment -- Drawing Part 1: Drawing stock closing price curve and ochl candle chart

How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of DBCD, ROC, vroc, Cr and psy index strategy income

Quantitative investment traditional index investment decision vs Monte Carlo simulation method
![[JS] battle chess](/img/1f/83ca6bcb000a5567dc6d3b72463ff8.jpg)
[JS] battle chess
![[JS component] floating text](/img/e5/7faad5422bba919bed34e3dbcf7ba0.jpg)
[JS component] floating text
![[JS component library] drag sorting component](/img/f9/4090b52da1a5784b834cb7dbbb948c.jpg)
[JS component library] drag sorting component

pytorch是什么?解释pytorch的基本概念
随机推荐
今日睡眠质量记录74分
Canvas airplane game
How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of strategic returns of vrsi, bbiboll, WR, bias and RSI indicators
Unity calls alertdialog
Characteristics of transactions -- atomicity (implementation principle)
Plusieurs catégories de tests logiciels sont claires à première vue
Rotating camera
The tle4253gs is a monolithic integrated low dropout tracking regulator in a small pg-dso-8 package.
(01). Net Maui actual construction project
Four startup modes of kotlin collaboration
[JS component library] drag sorting component
在国企做软件测试工程师是一种什么样的体验:每天过的像打仗一样
Common skills for quantitative investment - drawing 3: drawing the golden section line
Aof persistence
Common skills of quantitative investment - index part 2: detailed explanation of BOL (Bollinger line) index, its code implementation and drawing
【北亚服务器数据恢复】虚拟机文件丢失导致Hyper-V服务瘫痪的数据恢复案例
[server data recovery] successful cases of data loss recovery during data migration between storage servers
Matrix fast power
Binary tree -- using hierarchical sequence and middle sequence to determine a tree
5G工业网关在煤矿行业的应用优势