算法1
整个进行翻转,再翻转单词
C++ 代码
class Solution {
public:
string reverseWords(string s) {
reverse(s.begin(),s.end());
s.push_back(' ');
for(int i = 0; i < s.size();){
int r = i,l = i;
while(s[r] != ' ') r++;
i = r+1;
while(l < r ){
swap(s[l++],s[r--]);
}
}
return s.substr(1);
}
};