Reverse Words in a String
class Solution { public: void reverseWords(string &s) { if(s.size()==0) return ; int i=0,j=s.size()-1; vector<string> res; while((i<=j)&&s[i]==' ')i++; while((i<=j)&&s[j]==' ')j--; while(i<=j) { int tmp=i; while((i<=j)&& s[i]!=' ')i++; res.push_back(s.substr(tmp,i-tmp)); while((i<=j)&&s[i]==' ')i++; } s=""; if(res.empty()) return; reverse(res.begin(),res.end()); s=res[0]; for(vector<string>::iterator it=++res.begin();it!=res.end();it++) s+=" "+*it; } };