https://leetcode-cn.com/problems/reverse-words-in-a-string/
TLE代码:
class Solution { public: string reverseWords(string s) { stringstream ss(s); string buf; vector<string> ans; while(ss<<buf){ ans.push_back(buf); } for(vector<string>::iterator it = ans.end()-1;it==ans.begin();it++){ cout<< *it<<" "; } return 0; } };
亲测用stringstream确实挺慢的.
可以先用reverse 逆序,再挨个单词逆序回来.