题解 | #翻转单词序列#
翻转单词序列
https://www.nowcoder.com/practice/3194a4f4cf814f63919d0790578d51f3
class Solution { public: string ReverseSentence(string str) { string result; stack<char> st1; stack<char> st2; for (auto s : str) st1.push(s); while (!st1.empty()){ while (!st1.empty() && st1.top() != ' '){ st2.push(st1.top()); st1.pop(); } while (!st2.empty()){ result += st2.top(); st2.pop(); } if (!st1.empty()){ if (st1.top() == ' '){ result += st1.top(); st1.pop(); } } } return result; } };