LeetCode 151. 翻转字符串里的单词
原题链接
中等
作者:
lmz1111111
,
2020-05-21 11:34:08
,
所有人可见
,
阅读 737
class Solution {
public:
string reverseWords(string s) {
if(s=="") return s;
s+= ' ';
stack<string> cache;
string output = "";
string temp = "";
for(int i =0 ; i < s.length(); i++){
if(s[i] == ' '){
if(temp != ""){cache.push(temp);}
temp = "";
}
else{
temp += s[i];
}
}
while(! cache.empty()){
output += cache.top();
output += ' ';
cache.pop();
}
if (output =="") return output;
output.pop_back();
return output;
}
};