验证图书取出顺序 (验证图书取出顺序)

 

思路:

// @Title: 验证图书取出顺序 (验证图书取出顺序)
// @Author: qisiii
// @Date: 2022-02-21 12:01:12
// @Runtime: 3 ms
// @Memory: 40.7 MB
// @comment: 
// @flag: 
class Solution {
    public static boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack=new Stack();int count=0;
        for (int i : pushed) {
            stack.push(i);
            //如果等于栈顶,那就弹出去,等待下一位;
            while (!stack.isEmpty()&&popped[count]==stack.peek()){
                stack.pop();
                count++;
            }
        }
        return stack.isEmpty();
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18