JZ21 栈的压入弹出序列
本文最后更新于:2022年4月9日 中午
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class Solution { public: bool IsPopOrder(vector<int> pushV,vector<int> popV) { if (pushV.size() != popV.size()) return false; stack<int> stack; int i=0, j=0; while (i < pushV.size()) { if (pushV[i] != popV[j]) { stack.push(pushV[i++]); } else { ++i, ++j; while (!stack.empty() && stack.top() == popV[j]) { stack.pop(); ++j; } } } return stack.empty(); } };
|