标签:
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
class Solution { public: void reverseWords(string &s) { if(s.empty())return; string tmp=""; string str=""; int i=0; while(i<s.length()) { while(s[i]==‘ ‘&&i<s.length())i++; if(s[i]!=‘ ‘&&i<s.length()) { while(s[i]!=‘ ‘&&i<s.length())tmp=s[i++]+tmp; str+=(tmp+‘ ‘); tmp=""; } } reverse(str.begin(),str.end()); int n=str[0]==‘ ‘?1:0; s=str.substr(n,str.size()); return; } };
leetcode[151]Reverse Words in a String
标签:
原文地址:http://www.cnblogs.com/Vae98Scilence/p/4280723.html