题解 | #栈的压入、弹出序列#

栈的压入、弹出序列

https://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param pushV int整型一维数组 
 * @param popV int整型一维数组 
 * @return bool布尔型
 */
let stack = []
function IsPopOrder( pushV ,  popV ) {
    // write code here
    //循环压栈,判断这个压栈的元素是否和栈顶相等,相等的话就出栈,将出栈顺序像后面移一位,当不相等的时候,遍历完成
    let i = 0
    let j = 0
    pushV.forEach(item=>{
        stack.push(item)
        while(stack[stack.length-1] === popV[j]&&stack.length!==0){
            stack.pop()
            j++
        }
        i++
    })
    return stack.length===0
}
module.exports = {
    IsPopOrder : IsPopOrder
};

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务