面试题31. 栈的压入、弹出序列

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

func validateStackSequences(_ pushed: [Int], _ popped: [Int]) -> Bool {

    var stack = [Int]()
    var popIdx = 0
    
    for item in pushed {
        stack.append(item)
        while !stack.isEmpty && (popIdx < popped.count) && stack.last == popped[popIdx] {
            stack.removeLast()
            popIdx = popIdx + 1
        }
    }
    return stack.isEmpty
}

你可能感兴趣的:(面试题31. 栈的压入、弹出序列)