剑指Offer-29栈的压入、弹出序列

public static boolean validateStackSequences(int[] pushed, int[] popped) {
    // 双指针模拟
    int p1 = 0;
    int p2 = 0;
    Stack<Integer> stack = new Stack<>();
    while (p2 != popped.length){
        // 符合栈顶跟p2指针相等就出栈并且指针后移
        if (!stack.empty() && stack.peek().equals(popped[p2])){
            stack.pop();
            p2++;
        }else {
            // p1越界且不符合上面条件 走不动了
            if (p1 >= pushed.length){
                return false;
            }
            // 不符合上面条件 进栈且指针后移
            stack.push(pushed[p1]);
            p1++;
        }
    }
    return true;
}

思路更简单 一个栈就搞定了

public static boolean validateStackSequences(int[] pushed, int[] popped){
    Stack<Integer> stack = new Stack<>();
    int i = 0;
    for(int num : pushed) {
        stack.push(num); // num 入栈
        while(!stack.isEmpty() && stack.peek() == popped[i]) { // 循环判断与出栈
            stack.pop();
            i++;
        }
    }
    return stack.isEmpty();
}

猜你喜欢

转载自blog.csdn.net/a792396951/article/details/113843006