classSolution{public:
string reverseWords(string s){int i =0;int n = s.size();
string temp,result;while( i < n ){while(( i < n )&& s[i]==' ')
i++;if( i == n )break;
temp ="";while(( i < n )&& s[i]!=' ')
temp += s[i++];
result = temp +' '+ result;}//去掉刚开始加上的多余的空格if(!result.empty())
result.pop_back();return result;}};
方法二:使用istringstream模拟输入
classSolution{public:
string reverseWords(string s){
istringstream iss(s);
string str,result;while( iss >> str ){
result = str +' '+ result;}return result.substr(0, result.size()-1);}};
方法一:顺序处理字符串思路class Solution {public: string reverseWords(string s) { int i = 0; int n = s.size(); string temp,result; while( i < n ) { while( ( i < n ) && s[i] == ' ' ) .