其中用到stringstream 把char类型转换为string
然后就是一个栈。
class Solution {
public:
string reverseWords(string s) {
string s2="";
stack<string> sta;
for(int i=0;i<s.size();i++){
if(s[i]==' '){
while(!sta.empty()){
string x = sta.top();
sta.pop();
s2+=x;
}
s2+=" ";
}
else {
string str;
stringstream stream;
stream << s[i];
str = stream.str();
sta.push(str);
}
}
while(!sta.empty()){
string x = sta.top();
sta.pop();
s2+=x;
}
return s2;
}
};
490

被折叠的 条评论
为什么被折叠?



