翻转单词顺序列
C++,翻转单词,先把整体翻转,再单独翻转就好了。
class Solution {
public:
string ReverseSentence(string str) {
reverse(str.begin(), str.end());
int i = 0, j = 0;
for ( ; i < str.length(); ) {
for ( j = i; j < str.length() && str[j] != ' '; j++) ;
reverse(str.begin() + i, str.begin() + j);
i = j + 1;
}
return str;
}
};