Reverse Words in a String
Posted RenewDo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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; } };
以上是关于Reverse Words in a String的主要内容,如果未能解决你的问题,请参考以下文章
186. Reverse Words in a String II
LeetCode Reverse Words in a String III
4.Reverse Words in a String III
Reverse Words in a String leetcode