题目
解题:贪心
代码参考:面试题31. 栈的压入、弹出序列(模拟,清晰图解)
// javascript
var validateStackSequences = function(pushed, popped) {
const n = pushed.length;
const stk = [];
let i = 0;
for (const num of pushed) {
stk.push(num); // num 入栈
while (stk.length > 0 && i < n && stk[stk.length - 1] === popped[i]) { // 循环判断与出栈
stk.pop();
i++;
}
}
return stk.length === 0;
};