class Solution {
public:
void reverseWords(string &s) {
string word;
stack<string> wordsStack;
stringstream ss(s);
while(ss>>word)
{
wordsStack.push(word);
}
string res = "";
bool first = true;
while(!wordsStack.empty())
{
if(!first)
res += " ";
res += wordsStack.top();
wordsStack.pop();
first = false;
}
s = res;
}
};
[LeetCode]Reverse Words in a String
最新推荐文章于 2017-09-23 19:08:14 发布