栈题目:验证栈序列

题目

标题和出处

标题:验证栈序列

出处:946. 验证栈序列

难度

5 级

题目描述

要求

给定 pushed \texttt{pushed} pushed popped \texttt{popped} popped 两个序列,每个序列中的值都不重复,只有当它们可能是在最初空栈上进行的推入和弹出操作序列的结果时,返回 true \texttt{true} true;否则,返回 false \texttt{false} false

示例

示例 1:

输入: pushed   =   [1,2,3,4,5],   popped   =   [4,5,3,2,1] \texttt{pushed = [1,2,3,4,5], popped = [4,5,3,2,1]} pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
输出: true \texttt{true} true
解释:我们可以按以下顺序执行:
push(1),   push(2),   push(3),   push(4), \texttt{push(1), push(2), push(3), push(4),} push(1), push(2), push(3), push(4),
pop() → 4, \texttt{pop()} \rightarrow \texttt{4,} pop()4,
push(5), \texttt{push(5),} push(5),
pop() → 5,   pop() → 3,   pop() → 2,   pop() → 1 \texttt{pop()} \rightarrow \texttt{5, pop()} \rightarrow \texttt{3, pop()} \rightarrow \texttt{2, pop()} \rightarrow \texttt{1} pop()5, pop()3, pop()2, pop()1

示例 2:

输入: pushed   =   [1,2,3,4,5],   popped   =   [4,3,5,1,2] \texttt{pushed = [1,2,3,4,5], popped = [4,3,5,1,2]} pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
输出: false \texttt{false} false
解释: 1 \texttt{1} 1 不能在 2 \texttt{2} 2 之前弹出。

数据范围

  • 1 ≤ pushed.length ≤ 1000 \texttt{1} \le \texttt{pushed.length} \le \texttt{1000} 1pushed.length1000
  • 0 ≤ pushed[i] ≤ 1000 \texttt{0} \le \texttt{pushed[i]} \le \texttt{1000} 0pushed[i]1000
  • pushed \texttt{pushed} pushed 的所有元素互不相同
  • popped.length = pushed.length \texttt{popped.length} = \texttt{pushed.length} popped.length=pushed.length
  • popped \texttt{popped} popped pushed \texttt{pushed} pushed 的一个排列

解法

思路和算法

由于 pushed \textit{pushed} pushed 的所有元素互不相同,因此在任意时刻,栈内不可能出现两个相等的元素。

元素按照 pushed \textit{pushed} pushed 中的顺序依次入栈,按照 popped \textit{popped} popped 中的顺序依次出栈,出栈操作可能发生在任意时刻。由于只有当一个元素已经入栈且位于栈顶时,该元素才可能出栈,因此出栈操作发生的时刻一定是栈顶元素等于 popped \textit{popped} popped 中的当前元素的时刻。由此可以模拟入栈和出栈的操作,判断给定的 pushed \textit{pushed} pushed popped \textit{popped} popped 是否为有效的栈操作序列。

初始时, pushed \textit{pushed} pushed popped \textit{popped} popped 的下标都是 0 0 0。模拟的做法如下。

  1. pushed \textit{pushed} pushed 的当前元素入栈,并将 pushed \textit{pushed} pushed 的下标加 1 1 1

  2. 判断栈顶元素是否等于 popped \textit{popped} popped 的当前元素:

    • 如果相等,则将栈顶元素出栈,并将 popped \textit{popped} popped 的下标加 1 1 1,重复该过程直到栈为空或者栈顶元素不等于 popped \textit{popped} popped 的当前元素;

    • 如果不相等,则回到第 1 步。

重复上述操作,直到 pushed \textit{pushed} pushed popped \textit{popped} popped 当中至少有一个数组的下标超出数组的下标范围。

如果 popped \textit{popped} popped 的下标超出范围,则全部元素都已经出栈,由于每个元素都必须在入栈之后才能出栈,因此全部元素都一定有过入栈,即 pushed \textit{pushed} pushed 的元素全部遍历过,可知 pushed \textit{pushed} pushed 的下标也超出范围。因此不存在 popped \textit{popped} popped 的下标超出范围时 pushed \textit{pushed} pushed 的下标不超出范围的情况,即上述操作结束时, pushed \textit{pushed} pushed 的下标一定超出范围,即 pushed \textit{pushed} pushed 的元素全部遍历过。

由于上述操作结束时, pushed \textit{pushed} pushed 的元素全部遍历过,因此其中的元素都一定有过入栈。由于 pushed \textit{pushed} pushed popped \textit{popped} popped 的长度相同,对于有效的栈操作序列,入栈次数和出栈次数相同,最终结果是栈为空。因此当上述操作结束时,如果栈为空则返回 true \text{true} true,否则返回 false \text{false} false

还有其他的依据可以判断是否为有效的栈操作系列。由于 popped \textit{popped} popped 的每个元素都出栈,因此对于有效的栈操作序列,当上述操作结束时, popped \textit{popped} popped 的下标一定超出下标范围。因此另一种判断方法是,当上述操作结束时,如果 popped \textit{popped} popped 的下标超出下标范围则返回 true \text{true} true,否则返回 false \text{false} false

代码

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

复杂度分析

  • 时间复杂度: O ( n ) O(n) O(n),其中 n n n 是数组 pushed \textit{pushed} pushed popped \textit{popped} popped 的长度。需要遍历数组 pushed \textit{pushed} pushed popped \textit{popped} popped 各一次,每个元素最多入栈和出栈各一次。

  • 空间复杂度: O ( n ) O(n) O(n),其中 n n n 是数组 pushed \textit{pushed} pushed popped \textit{popped} popped 的长度。空间复杂度主要取决于栈空间,栈内的元素个数为 O ( n ) O(n) O(n)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

伟大的车尔尼

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

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

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

打赏作者

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

抵扣说明:

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

余额充值