题目描述
代码
class Solution { void mswap(string &str, int left, int right) { if (left >= right) return; while (left < right) { swap(str[left], str[right]); left++; right--; } } public: string ReverseSentence(string str) { string ret = str; int slen = ret.size(); int left = 0; for (int i=1; i<=slen; ++i) { if (ret[i] == ' ' || i == slen) { mswap(ret, left, i-1); left = i+1; } } mswap(ret, 0, slen-1); return ret; } };