LeetCode 946: Validate Stack Sequences (栈好题)

本文介绍了一种算法,用于验证给定的两个序列是否能通过一系列标准的压栈和出栈操作得到。通过两种不同的实现方法,详细解释了如何使用栈来模拟这一过程。

946. Validate Stack Sequences

Medium

Given two sequences pushed and popped with distinct values, return true if and only if this could have been the result of a sequence of push and pop operations on an initially empty stack.

 

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:

  • 0 <= pushed.length == popped.length <= 1000
  • 0 <= pushed[i], popped[i] < 1000
  • pushed is a permutation of popped.
  • pushed and popped have distinct values.


     

这题的思路就是用一个栈模拟进栈和出栈数字,并与popped序列比较。

比如pushed=[1,2,3,4,5],popped={4,5,3,2,1},先把1,2,3,4都压栈,然后发现4==popped[0],则4出栈,然后又把5压栈,5==popped[1],5出栈,然后3==popped[2], 2==popped[3], 1==popped[4],所以3,2,1分别出栈,最后栈空,所以合法。

但如果pushed=[1,2,3,4,5],popped={4,3,5,1,2},先把1,2,3,4都压栈,然后发现4==popped[0],则4出栈,然后又发现3==popped[1],3也出栈,然后5入栈,发现5==popped[2],5出栈,然后2入栈,1入栈,因为2!=popped[3] (1),所以没法继续pop了,最后栈里面还剩2,1,所以非法。
实现可以有2个思路,一个是遍历压栈序列,用while循环弹栈,另一个是遍历出栈序列,用while循环压栈。

解法1:遍历压栈序列,对每个数字,先压栈,然后用一个while循环:如果栈顶==当前出栈数字,则弹栈,出栈数字也往后移动一个,否则继续压栈。
 

class Solution {
public:
    bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
        int n = pushed.size();
        stack<int> s;
        int pop_index = 0;
        for (auto elem : pushed) {
            s.push(elem);
            while (!s.empty() && s.top() == popped[pop_index]) {
                s.pop();
                pop_index++;
            }
        }
        return s.empty();
    }
};

解法2:遍历出栈序列,对每个数字,先比较当前出栈数字和栈顶是否一样,否则用一个while循环不停压栈。出了while循环,说明2种可能:
1)当前栈顶==当前出栈数字,所以要弹栈,出栈数字往后移动一位
或者
2)数字全部压完了,这时候就应该返回false。
注意解法1有个好处就是因为是先压栈,所以判断栈空可以直接用s.empty(),而解法2是先弹栈,所以必须先压栈-1,判断栈空要用s.top() == -1。

class Solution {
public:
    bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
        int n = pushed.size();
        stack<int> s;
        s.push(-1);
        int pushed_index = 0, popped_index = 0;
        while (popped_index < n) {
            while (pushed_index < n && s.top() != popped[popped_index]) {
                s.push(pushed[pushed_index++]);
            }
            if (s.top() == popped[popped_index]) {
                s.pop();
                popped_index++;
            } else {
                return false;
            }
        }
        return s.top() == -1;
    }
};

也可以写成


class Solution {
public:
    bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
        int n = pushed.size();
        stack<int> s;
        s.push(-1);
        int push_index = 0;
        for (auto elem : popped) {
            while (push_index < n && s.top() != elem) {
                s.push(pushed[push_index++]);
            }
            if (s.top() == elem) {
                s.pop();
            } else {
                return false;
            }
        }
        return s.top() == -1;
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值