题解 | #翻转单词序列#
翻转单词序列
http://www.nowcoder.com/practice/3194a4f4cf814f63919d0790578d51f3
反转单词
【思路】:每个单词反转,然后整体反转
class Solution {
public:
string ReverseSentence(string str) {
if(!str.size())return "";
int pre = 0;
for(int i = 0;i < str.size();++i){
if(str[i] == ' '){
reverse(str.begin()+pre, str.begin()+i);
while(str[i] == ' ')++i;
pre = i;
}
}
reverse(str.begin()+pre, str.end());
reverse(str.begin(), str.end());
return str;
}
};