剑指Offer-1.18-31

在这里插入图片描述
在这里插入图片描述

class Solution {
    
    
    public boolean validateStackSequences(int[] pushed, int[] popped) {
    
    
        Stack<Integer> stack = new Stack<Integer>();
        for (int pushedIndex = 0, poppedIndex = 0; pushedIndex < pushed.length; pushedIndex++) {
    
    
            stack.push(pushed[pushedIndex]);
            while (!stack.isEmpty() && stack.peek() == popped[poppedIndex]) {
    
    
                stack.pop();
                poppedIndex++;
            }
        }
        return stack.isEmpty();
    }
}

猜你喜欢

转载自blog.csdn.net/Desperate_gh/article/details/112793885