0
点赞
收藏
分享

微信扫一扫

剑指 Offer 31. 栈的压入、弹出序列(javascript)

余寿 2022-01-25 阅读 26

一、题目地址

https://leetcode-cn.com/problems/zhan-de-ya-ru-dan-chu-xu-lie-lcof/

二、具体代码

/**
 * @param {number[]} pushed
 * @param {number[]} popped
 * @return {boolean}
 */
// 辅助栈
// 时间复杂度:O(N)
// 空间复杂度:O(N)
 var validateStackSequences = function(pushed, popped) {
    let stack = [];
    let i = 0;
    for(let num of pushed) {
        stack.push(num);
        while((stack.length !== 0) && (stack[stack.length - 1] === popped[i])) {
            stack.pop();
            i++;
        }
    }
    return stack.length === 0;
};

三、补充链接

https://leetcode-cn.com/problems/zhan-de-ya-ru-dan-chu-xu-lie-lcof/solution/mian-shi-ti-31-zhan-de-ya-ru-dan-chu-xu-lie-mo-n-2/

举报

相关推荐

0 条评论