leetcode 946. Validate Stack Sequences(有效栈序列)

本文探讨了如何通过编程判断两个整数数组,代表栈的推入和弹出操作,是否能够使栈最终恢复为空。两种高效算法分析及实现策略,适用于面试问题和栈操作理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given two integer arrays pushed and popped each with distinct values, return true if this could have been the result of a sequence of push and pop operations on an initially empty stack, or false otherwise.

Example 1:

Input: pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
Output: true
Explanation: We might do the following sequence:
push(1), push(2), push(3), push(4),
pop() -> 4,
push(5),
pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1

Example 2:

Input: pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
Output: false
Explanation: 1 cannot be popped before 2.

Constraints:

1 <= pushed.length <= 1000
0 <= pushed[i] <= 1000
All the elements of pushed are unique.
popped.length == pushed.length
popped is a permutation of pushed.

给两个数组,一个是要压栈的数字,一个是要出栈的数字。问经过这些出栈操作之后栈是否能恢复空。
即push, pop操作是否有效。

思路:
可用两个指针顺序指向pushed, popped中的数字,两个一样时直接跳过(不用先push再pop一遍),
不一样时先看stack中有没有可以pop的元素(即栈顶元素是否与pop元素相等),如果有,pop,然后pop指针右移;
如果没有, push入stack中,push指针右移(但是如果push已经到最右端,就返回挨个找栈顶是否有匹配元素,如果和栈顶不匹配,直接返回false)

//3ms
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        int n = pushed.length;
        Stack<Integer> stack = new Stack<>();
        int push = 0;
        int pop = 0;
        
        while(pop < n) {
            if(push >= n) {
                if(stack.isEmpty() || stack.peek() != popped[pop]) return false;
                else stack.pop();
                pop ++;
            } else {
                if(pushed[push] != popped[pop]) {
                    if(!stack.isEmpty() && stack.peek() == popped[pop]) {
                        stack.pop();
                        pop ++;
                    } else {
                       stack.push(pushed[push]);
                       push ++; 
                    }
                
                } else {
                    push ++;
                    pop ++;
               
                }
                
            }
            
            
        }
        return true;
    }

或者是每次都push, 然后先看stack中有没有可以pop的,一下pop完,再进入下一个push。

//4ms
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        int n = pushed.length;
        Stack<Integer> stack = new Stack<>();
        int pop = 0;
        
        for(int num : pushed) {
            stack.push(num);
            while(!stack.isEmpty() && stack.peek() == popped[pop]) {
                stack.pop();
                pop ++;
            }
        }
        return stack.isEmpty();
    }

数组模拟stack

//0ms
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        int n = pushed.length;
        int[] st = new int[n];
        int head = -1; //栈顶
        int pushP = 0;  //pushed pointer
        int popP = 0;   //popped pointer

        for(int pushNum : pushed) {
            st[++head] = pushed[pushP++];
            while(head >= 0 && st[head] == popped[popP]) {
                head --;
                popP ++;
            }
        }
        return (head == -1);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值