946 Validate Stack Sequences

16 篇文章 0 订阅

1 题目

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.

2 尝试解

2.1 分析

给定一组有序入栈操作pushed和一组有序出栈操作popped,pushed和popped互为排列组合。且同一组操作中操作元素都是唯一的,即某个元素不会两次出栈或两次入栈。问在初始栈为空栈的情况下,这两组出入栈操作是否兼容。

以pushed = [1,2,3,4,5], popped = [4,3,5,1,2]为例,最后一个入栈元素5把出栈数组划分为两部分,在5入栈之时,[4,3]就已出栈,[1,2]仍在栈中。

考虑已经出栈的元素,该子问题转化为入栈操作[3,4]和出栈操作[4,3]是否相容,如果不相容,直接返回False,否则继续检查另一部分仍在战中的元素。

考虑仍在栈中的元素[1,2],因为此时[1,2]都还在栈中,所以其操作顺序是在5入栈之前全部入栈,5出栈之后再全部出栈,检查其出入栈操作顺序是否完全相反即可。

2.2 代码

class Solution {
public:
    bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
        if(popped.size() == 0)
            return true;
        int last_push = pushed.back();
        vector<int> popped_out,pushed_out;
        stack<int> popped_in;
        int find = 0;
        for(auto&ele:popped){
            if(ele == last_push){
                find = 1;
                continue;
            }
            if(find){
                popped_in.push(ele);
            }
            else{
                popped_out.push_back(ele);
            }
        }
        for(int i = 0; i < pushed.size()-1; i++){
            if(popped_in.size() && pushed[i] == popped_in.top()){
                popped_in.pop();
            }
            else{
                pushed_out.push_back(pushed[i]);
            }
        }
        if(popped_in.empty())
            return validateStackSequences(pushed_out,popped_out);
        else 
            return false;
    }
};

3 标准解

3.1 分析

第一个出栈的元素,一定是刚入栈就出栈。所以按照入栈顺序依次将元素入栈,同时每入栈一个元素就检查是不是第一个要出栈的元素。如果是弹出该元素,同时检查新的栈顶元素是不是新的第一个要出栈的元素。

3.2 代码

class Solution {
public:
   bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
        stack<int> stack;                                       //comparison stack
        int i = 0;
        for (int x=0;x<pushed.size(); x++) {
            
            stack.push(pushed[x]);                              //push entries onto comparison stack
            
            
            while (stack.size() && stack.top() == popped[i]) {  //find out where the first pushed and pop matches are then work backwards from there
                stack.pop();                                    //pop off the matched entry
                i++;                                            //search for next match
                
            }
        }
        return stack.size() == 0;                               //it means that we popped all entries and found matches for all entries
    }
};

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值