zoukankan      html  css  js  c++  java
  • 剑指 Offer 31. 栈的压入、弹出序列

    输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如,序列 {1,2,3,4,5} 是某栈的压栈序列,序列 {4,5,3,2,1} 是该压栈序列对应的一个弹出序列,但 {4,3,5,1,2} 就不可能是该压栈序列的弹出序列。

    示例 1:

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

    示例 2:

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

    提示:

    1. 0 <= pushed.length == popped.length <= 1000
    2. 0 <= pushed[i], popped[i] < 1000
    3. pushed 是 popped 的排列。

    法一: 扣半小时的结果,左调右调,面试官是不会给你这个机会的。

    class Solution {
    public:
        bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
            stack<int>nums;
            while(!nums.empty()) nums.pop();
    
            int n = pushed.size();
            int i = 0, cnt = 0;
            while(cnt < n){
                while(cnt<n && i<n && pushed[i] != popped[cnt]){
                    nums.push(pushed[i]);
                    i++;
                }
                while(i<n && pushed[i] == popped[cnt]) nums.push(pushed[i++]);//if
                while(cnt<n && !nums.empty() && nums.top() == popped[cnt]){
                    nums.pop();
                    cnt++;
                }
                if(i == n && !nums.empty()){
                    return false;
                }
                cout << cnt << endl;
                
            }
            return true;
            
            
        }
    };

    法二:题解

    class Solution {
        public 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();
        }
    }
    
    作者:jyd
    链接:https://leetcode-cn.com/problems/zhan-de-ya-ru-dan-chu-xu-lie-lcof/solution/mian-shi-ti-31-zhan-de-ya-ru-dan-chu-xu-lie-mo-n-2/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

    自己改造一下:

    class Solution {
    public:
        bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
            stack<int>nums;
            while(!nums.empty()) nums.pop();
    
            int n = pushed.size(), cnt = 0;
            for(int i=0; i<n; i++){
                nums.push(pushed[i]);
                while(!nums.empty() && nums.top() == popped[cnt]){
                    nums.pop();
                    cnt++;
                }
            }
            if(!nums.empty()) return false;
            return true;
            
            
        }
    };

    时间复杂度 O(N): 其中 N为列表 pushed 的长度;每个元素最多入栈与出栈一次,即最多共 2N 次出入栈操作。
    空间复杂度 O(N): 辅助栈 stack最多同时存储 N 个元素。

    法三: 使用 pushed模拟栈

    转自

    class Solution {
        public boolean validateStackSequences(int[] pushed, int[] popped) {
            int i = 0, j = 0;
            for (int e : pushed) {
                pushed[i] = e;//太优秀了
                while (i >= 0 && pushed[i] == popped[j]) {
                    j++;
                    i--;
                }
                i++;
            }
            return i == 0;
        }
    }

    时间:O(n)

    空间:O(1)

    再改造一波:

    class Solution {
    public:
        bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
    
            int n = pushed.size(), cnt = 0;
            int i = 0, j = 0;
            for(auto e: pushed){
                pushed[i] = e;
                while(i>=0 && pushed[i] == popped[j]){
                    i--;
                    j++;
                }
                i++;
            }
            return i == 0;
        }
    };
  • 相关阅读:
    IPC总结学习
    机器学习中的范数规则
    机器学习的几个误区-转载
    来几道大数据的面试题吧
    海量数据随机抽样问题(蓄水池问题)
    字符串类算法题目总结
    RPC学习
    如何做出健壮的系统设计
    关于bind函数和connect函数的测试结论
    [置顶] Codeforces Round #197 (Div. 2)(完全)
  • 原文地址:https://www.cnblogs.com/AbsolutelyPerfect/p/14940523.html
Copyright © 2011-2022 走看看