[LeetCode]Reverse Words in a String

来源:互联网 发布:知乎 女生减肥 编辑:程序博客网 时间:2024/06/06 19:57
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;    }};

0 0
原创粉丝点击