【基本数据结构】剑指 Offer 31. 栈的压入、弹出序列

本问题对应课程链接:剑指offer刷题第四期

要求:

1、请打卡你的解题代码,如果能简单说一下思路则简单说一下思路。
2、给出你代码所用的时间复杂度和空间复杂度

共以下 42 个回答

  • 我家旺仔 普通 2023年8月1日 下午10:17

    打卡:
    直接发现规律 遍历popped数组,如果当前元素比其后一个元素在pushed中的index大,且大于1,直接返回false,否则在pushed数组中删掉相同元素
    class Solution {
    public:
        int index(vector& pushed,int data){
        int i = 0;
        for(i;i < pushed.size()-1; i++){
            if(pushed[i] data)
                break;
        }
        return i;
    }

    bool validateStackSequences(vector& pushed, vector& popped) {
            int data_size = pushed.size();
            int id = 0,after = 0,pre = 0;
    if(data_size 0|| data_size 1)
    return true;

            for(int i = 0; i < data_size-1;i++){
                id = index(pushed, popped[i]);
    after = index(pushed,popped[i+1]);
    if ((id-after) > 1)
    return false;
    else{
    pushed.erase(pushed.begin()+id);
    }
            }
               
            return true;
        }

    };

    0 赞同 0 条回复
  • 今天没时间刷题 普通 2023年7月26日 下午10:15
    时间On,空间On
    class Solution {
    public:
        bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
            if(pushed.empty())
            {
                return true;
            }
            stack<int> tempStack;
            int i{0};
            for(const auto& it : pushed)
            {
                tempStack.push(it);
                while(!tempStack.empty() && tempStack.top() == popped[i])
                {
                    tempStack.pop();
                    ++i;
                }
            }
            return tempStack.empty();
        }
    };
    
    0 赞同 0 条回复
  • hi 普通 2023年7月25日 下午3:55
    1. 两个数组都得用上,去模拟压入和弹出的过程
    2. 新建一个栈,对压入数组中的数逐个压入,同时如果压入栈中的数与popped数组里的数相同则弹出
    class Solution {
    public:
        bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
            //模拟一遍过程
            if(pushed.size() != popped.size()) return false;
    
            stack<int> stk1;
            int index = 0; 
            for(int i = 0; i < pushed.size(); i++)
            {
                stk1.push(pushed[i]);
                while(!stk1.empty() && stk1.top() == popped[index])
                {
                    stk1.pop();
                    index++;
                }
            }
            return stk1.empty();
        }
    };
    
    0 赞同 0 条回复
  • wowo 普通 2023年7月23日 下午3:30

    模拟栈的压入弹出,将栈顶元素与弹出序列比较,如果有相同的就pop栈顶,弹出序列索引+1,一直到没有相同的元素
    时间复杂度O(n),空间复杂度O(n)

    class Solution:
        def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
            stack = []
            i = 0
    
            for ele in pushed:
                stack.append(ele)
                while stack and stack[-1] == popped[i]:
                    stack.pop()
                    i += 1
    
            return not stack
    
    0 赞同 0 条回复
  • 新禾 普通 2023年7月22日 下午6:05

    // 时间复杂度on 空间复杂度on 模拟
    class Solution {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
    Stack stack = new Stack();
    int i = 0;
    for(int num : pushed) {
    stack.push(num);
    while(!stack.isEmpty() && stack.peek() popped[i]) {
    stack.pop();
    i++;
    }
    }
    return stack.isEmpty();
    }
    }

    0 赞同 0 条回复
  • finalrgy 普通 2023年7月16日 下午5:29
    func validateStackSequences(pushed []int, popped []int) bool {
    
        indexM := make(map[int]int, len(pushed))
        for i, v := range pushed {
            indexM[v] = i + 1
        }
    
        var maxI int
        s := make([]int, 0, len(pushed))
        for _, v := range popped {
            i := indexM[v]
            if i > maxI {
                for k := maxI + 1; k < i; k++ {
                    s = append(s, k)
                }
                maxI = i
            } else {
                if i != s[len(s)-1] {
                    return false
                }
                s = s[:len(s)-1]
            }
        }
    
        return true
    }
    
    1. 直接模拟出栈,用一个数组存放栈中剩余的数据
    2. 时间O(N) 空间O(N)
    0 赞同 0 条回复
  • 神经蛙 普通 2023年7月16日 上午11:05

    时间复杂度O(n)
    空间复杂度O(n)

    class Solution {
        public boolean validateStackSequences(int[] pushed, int[] popped) {
            LinkedList<Integer> stack = new LinkedList<>();
            for (int i = 0, j = 0; i < pushed.length; ++i) {
                // 模拟入栈
                stack.add(pushed[i]);
                // 模拟出栈
                while (!stack.isEmpty() && stack.getLast() == popped[j]) {
                    stack.removeLast();
                    ++j;
                }
            }
            return stack.isEmpty();
        }
    }
    
    0 赞同 0 条回复
  • 朱志刚 普通 2023年7月15日 下午10:26
    class Solution {
    public:
        bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
            stack<int> t;
            int k=0;
            for(int i=0;i<pushed.size();i++)
            {
                t.push(pushed[i]);
                while(!t.empty()&&t.top()==popped[k])
                {
                    t.pop();
                    k++;
                }
    
    
            }
            if(t.empty())return true;
            else
                return false;
    
    
        }
    };
    
    

    测试通过

    0 赞同 0 条回复
  • ppopp 普通 2023年7月15日 下午7:57

    class Solution {
    public:
    bool validateStackSequences(vector& pushed, vector& popped) {
    //用一个栈模拟出入栈情况
    stack
    stk;
    int n = pushed.size();
    for(int i = 0, j = 0; i < n; i++){
    stk.emplace(pushed[i]);
    while(!stk.empty() && stk.top() popped[j]){
    stk.pop();
    j++;
    }
    }
    return stk.empty();
    }
    };

    0 赞同 0 条回复
  • 周小二 普通 2023年7月15日 下午4:56
    class Solution {
        //模拟栈
        public boolean validateStackSequences(int[] pushed, int[] popped) {
            Stack<Integer> stack = new Stack<>();
            for(int i = 0, j= 0; i < pushed.length; i++) {
                stack.push(pushed[i]);
                //如果模拟栈栈不为空且栈顶元素等于pop序列当前元素,弹出栈顶元素,pop序列+1
                while(!stack.isEmpty() && stack.peek() == popped[j]) {
                    stack.pop();
                    j++;
                }
            }
            return stack.isEmpty();
        }
    }
    
    0 赞同 0 条回复
  • 今天你刷题了嘛 普通 2023年7月14日 下午11:02
    class Solution {
        public boolean validateStackSequences(int[] pushed, int[] popped) {
            if(pushed==null||pushed.length==0) return true;
            Stack<Integer> stcak = new Stack<>();
            // 模拟进栈,出栈,先入栈,再出栈
            int j=0;
            for(int i=0;i<pushed.length;i++){
                stcak.push(pushed[i]);
                // 判断出栈
                while(!stcak.isEmpty()&&stcak.peek()==popped[j]){
                    stcak.pop();
                    j++;
                }
            }
            if(stcak.isEmpty()) return true;
            else return false;
        }
    }
    

    时间复杂度:O(n)
    空间复杂度:O(n)

    0 赞同 0 条回复
  • 长河呼叫黄江 普通 2023年7月14日 下午10:12

    【补卡】Day5:下面是代码

    class Solution {
        public boolean validateStackSequences(int[] pushed, int[] popped) {
            Stack<Integer> stack = new Stack<>();
            int i = 0;
            for(int num : pushed){
                stack.push(num);
                while(!stack.isEmpty() && stack.peek() == popped[i]){
                    stack.pop();
                    i++;
                }
            }
            return stack.isEmpty();
        }
    }
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2023年7月14日 下午3:39

    直接模拟即可

    /*
     * @lc app=leetcode.cn id=946 lang=cpp
     *
     * [946] 验证栈序列
     */
    #include<vector>
    using namespace std;
    // @lc code=start
    class Solution {
    public:
        bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
          vector<int> stack;
          auto p = popped.begin();
          for (auto i = pushed.begin(); i != pushed.end(); i++) {
            stack.push_back(*i);
            while(!stack.empty()&&stack.back()==*p){
              stack.pop_back();
              p++;
            }
          }
          if(p!=popped.end()||!stack.empty())
            return false;
        else
            return true;
        }
    };
    // @lc code=end
    
    
    
    0 赞同 0 条回复
  • Marshan 普通 2023年7月14日 下午2:37

    做之前一点思路没有看了帅地视频结合GPT基本掌握了

    class Solution {
    public:
        bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
            if(pushed.empty()){
                return true;
            }
            int k = 0;
            stack<int> stack;
            for(int i = 0;i < pushed.size();++i){
                stack.push(pushed[i]);
                while(!stack.empty() && stack.top()==popped[k]){
                    stack.pop();
                    k++;
                }
            }
            return stack.empty();
        }
    };
    
    0 赞同 0 条回复
  • 枫梓。 普通 2023年7月14日 下午1:29
    class Solution {
    public:
        bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
            stack<int> stk;
            int k = 0;
            for(int i = 0; i < pushed.size(); ++i) {
                stk.emplace(pushed[i]);
                while(!stk.empty() && popped[k]==stk.top()) {
                    stk.pop();
                    ++k;
                }
            }
            return stk.empty();
        }
    };
    

    用辅助栈模拟即可。双O(n)

    0 赞同 0 条回复
  • 片言莫语 普通 2023年7月14日 上午11:08
    class Solution {
        public boolean validateStackSequences(int[] pushed, int[] popped) {
            // 特殊情况判断
            if(pushed.length == 0 || pushed == null){
                return true;
            }
            int k = 0;
            Stack<Integer> stack = new Stack<>();
            for(int i = 0;i<pushed.length;i++){
                stack.push(pushed[i]);  //数组pushed中的元素入栈
                while(!stack.isEmpty() && stack.peek() == popped[k]){
                    //栈不为空,并且栈顶元素和出栈的第k元素相等,则栈中元素出栈
                    stack.pop();
                    k++;    //出栈的元素下标继续后移
                }
            }
            return stack.isEmpty(); //如果栈空了,则说明配对成功
        }
    }
    
    0 赞同 0 条回复
  • Mby 普通 2023年7月14日 上午10:28
    class Solution {
        public boolean validateStackSequences(int[] pushed, int[] popped) {
            if(pushed == null || pushed.length <= 0){
                return true;
            }
            int k = 0;
            Stack<Integer> stack = new Stack();//辅助站
            for(int i = 0; i < pushed.length; i++){
                stack.push(pushed[i]);
                while(!stack.isEmpty() && stack.peek() == popped[k]){
                    stack.pop();
                    k++;
                }
            }
             return stack.isEmpty();
    }
    }
    
    
    
    0 赞同 0 条回复
  • Fighting boy😀 普通 2023年7月13日 下午10:58

    // C++实现
    class Solution {
    public:
    bool validateStackSequences(vector& pushed, vector& popped) {
    if (pushed.empty()) {
    return true;
    }
    int k = 0;
    stack
    s;
    for (int i = 0; i < pushed.size(); ++i) {
    s.push(pushed[i]);
    while (!s.empty() && s.top() popped[k]) {
    s.pop();
    ++k;
    }
    }
    return s.empty();
    }
    };

    0 赞同 0 条回复
  • #00456B 普通 2023年7月13日 下午4:54

    用一个stack来做辅助,先向栈中压入pushed[i],然后判断poped[j]stk.top()是否成立,即当前栈顶是否满足弹出条件

    class Solution {
    public:
        bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
            stack<int> stk;
            int j = 0;
            for(int i = 0;i<pushed.size();i++){
                stk.push(pushed[i]);
                while(!stk.empty() && stk.top() == popped[j]){
                    stk.pop();
                    j++;
                }
            }
            return stk.empty();
        }
    };
    
    0 赞同 0 条回复
  • 北辰 普通 2023年7月13日 上午10:41
    int n = pushed.length;
            Stack<Integer> s = new Stack<>();
            int k=0;
            for(int i=0;i<n;i++) {
                s.push(pushed[i]);
                while(!s.isEmpty()&&s.peek()==popped[k]) {
                    s.pop();
                    k++;
                }
            }
            return s.isEmpty();
    

    时间复杂度:O(n)
    空间复杂度:O(n)

    0 赞同 0 条回复
  • CaCO3 永久会员 2023年7月12日 下午9:04

    ”’java
    class Solution {
    public boolean validateStackSequences(int[] pushed, int[] popped) {

        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for(int i = 0;i < pushed.length;i++){
            stack.push(pushed[i]);
            while(j <= popped.length - 1 && !stack.isEmpty() && stack.peek() == popped[j]){
                stack.pop();
                j++;
            }
    
        }
        for(int i = j;i < pushed.length;i++){
            if(stack.pop() != popped[i]){
                return false;
            }
        }
        return true;
    }
    

    }
    ”’
    时间复杂度O(n^2)
    空间复杂度O(n)

    0 赞同 0 条回复
  • H、 普通 2023年7月12日 下午8:13

    栈的压入、弹出序列

     class Solution {
        public boolean validateStackSequences(int[] pushed, int[] popped) {
            if(pushed.length == 0 && popped.length == 0){
                return true;
            }
            Stack<Integer> stack = new Stack<>();
    
            int poppedIndex = 0;
            for (int num : pushed) {
                stack.push(num);
                while (!stack.isEmpty() && stack.peek() == popped[poppedIndex]){
                    stack.pop();
                    poppedIndex++;
                }
            }
            return stack.isEmpty();
    
        }
    }
    
    0 赞同 0 条回复
  • NullPointerException 普通 2023年7月12日 下午7:21

    class Solution {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
    //可以使用一个辅助栈 遍历pushed推进,顺序对poped对比,如果能比上就出
    //当pushed全部进入栈里,但出去顺序不是poped顺序 就false
    //其实就是根据poped的顺序出栈
    if(pushednull||pushed.length0)
    {
    return true;
    }
    Stack stack =new Stack();
    int j=0;
    for(int i = 0;i<pushed.length;i++)
    {
    stack.push(pushed[i]);
    while(stack.size()>0&&stack.peek()popped[j])
    {
    stack.pop();
    j++;
    }
    }
    if(stack.size()
    0)
    {
    return true;
    }else
    {
    return false;
    }
    }
    }

    0 赞同 0 条回复
  • 人生何处不青山 普通 2023年7月12日 下午5:25

    TS写的

    function validateStackSequences(pushed: number[], popped: number[]): boolean {
        let stack = [];
        let idx = 0;
        for(let i of pushed) {
            stack.push(i);
            if(i === popped[idx]) {
                while(stack.length && stack.slice(-1)[0] === popped[idx]) {
                    idx++;
                    stack.pop();
                }
            }
        }
        return !stack.length;
    };
    
    0 赞同 0 条回复
  • momo 普通 2023年7月12日 下午4:03

    时间复杂度O(n)
    空间复杂度O(n)

    class Solution {
        public boolean validateStackSequences(int[] pushed, int[] popped) {
            Deque<Integer> stack = new ArrayDeque<>();
            int j = 0;
            for (int i = 0; i < pushed.length; i++) {
                stack.push(pushed[i]);
                while (!stack.isEmpty() && stack.peek() == popped[j]) {
                    stack.pop();
                    j++;
                }
            }
            return j == popped.length;
        }
    }
    
    0 赞同 0 条回复
  • AD钙奶 普通 2023年7月12日 下午3:48
    class Solution {
        Deque<Integer> deque = new ArrayDeque<>();
        public boolean validateStackSequences(int[] pushed, int[] popped) {
            int index = 0;
            for(int i = 0; i < pushed.length; i ++) {
                deque.addFirst(pushed[i]);
                while(!deque.isEmpty() 
                        && index < popped.length 
                        && deque.peekFirst() == popped[index]) {
                    deque.pop();
                    index ++;
                }
            }
            return deque.isEmpty();
        }
    }
    

    时间复杂度:O(n)
    空间复杂度:O(n)

    0 赞同 0 条回复
  • doordie. 普通 2023年7月12日 上午8:46

    栈的压入、弹出

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

    时间复杂度:O(n) 其实n是列表pushed的长度,每个元素最多入栈一次出栈一次,总共2n次
    空间复杂度:O(n) 辅助栈有n个元素

    0 赞同 0 条回复
  • 艺 普通 2023年7月12日 上午12:05

    “`c++
    //时间复杂度:O(n)
    //空间复杂度:O(n)
    class Solution {
    public:
    bool validateStackSequences(vector& pushed, vector& popped) {
    if(pushed.size()<=0) return true; int k=0; stack
    s;
    int i;
    for(i= 0;i < pushed.size();i++) { s.push(pushed[i]); while(! s.empty()&& s.top()= = popped[k]) { s.pop(); k++; } } return s.empty(); } };

    0 赞同 0 条回复
  • wonderful 普通 2023年7月11日 下午11:41

    class Solution:
    def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
    stack, i = [], 0
    for num in pushed:
    stack.append(num) # num 入栈
    while stack and stack[-1] popped[i]: # 循环判断与出栈
    stack.pop()
    i += 1
    return not stack

    0 赞同 0 条回复
  •  普通 2023年7月11日 下午11:33

    c++ 代码
    class Solution {
    stack instack;
    public:
    bool validateStackSequences(vector
    & pushed, vector& popped)
    {
    int count=0;
    for(int i=0;i<pushed.size();i++)
    {
    instack.push(pushed[i]);
    while(!instack.empty()&&instack.top()popped[count])
    {
    instack.pop();
    count++;
    }
    }
    return instack.empty();
    }
    };

    0 赞同 0 条回复
  • 土豆芋泥包_ 普通 2023年7月11日 下午10:38

    代码

    class Solution {
        public boolean validateStackSequences(int[] pushed, int[] popped) {
            if(pushed==null||pushed.length<=0){
                return true;
            }
    
            Stack<Integer> stack = new Stack();
            int k=0;
            for(int i=0;i<pushed.length;i++){
                stack.push(pushed[i]);
                while(!stack.isEmpty()&&stack.peek()==popped[k]){
                    stack.pop();
                    k++;
                }
            }
            return stack.isEmpty();
        }
    }
    
    0 赞同 0 条回复
  • ONE PIECE 普通 2023年7月11日 下午9:48
    class Solution {
        public boolean validateStackSequences(int[] pushed, int[] popped) {
            Stack<Integer> stack = new Stack<>();
            int j=0;
            for(int i = 0;i<pushed.length;i++){
                stack.push(pushed[i]);
                while(!stack.isEmpty() && stack.peek()==popped[j]){
                    j++;
                    stack.pop();
                }
            }
            return stack.isEmpty();
        }
    }
    

    时间复杂度:O(N);
    空间复杂度:O(N);

    0 赞同 0 条回复
  • 远行客 普通 2023年7月11日 下午9:33
    class Solution:
        def validateStackSequences(self, pushed: list[int], popped: list[int])-> bool:
            """
            1。 pushed中元素互不相同
            2。 pushed 和 popped 的长度相同
            3。 popped 是 pushed 的一个排列
            """
            st = []
            j = 0
            for x in pushed:
                # 入栈
                st.append(x)
    
                # pushed栈顶元素与popped当前元素相同
                while st and st[-1] == popped[j]:
                    # 出栈
                    st.pop()
                    j += 1
    
            # 经过所有的入栈和出栈操作, 栈为空,
            # 则说明pushed和popped是有效的栈操作序列
            return len(st) == 0
    
    """
    时间复杂度:O(N)
    空间复杂度:O(N)
    """
    
    
    0 赞同 0 条回复
  • 阿尚 普通 2023年7月11日 下午8:12
    class Solution {
        public boolean validateStackSequences(int[] pushed, int[] popped) {
            Stack<Integer> stack = new Stack<>();
    
            int i = 0;
            for(int num : pushed){
                stack.push(num);
                while(!stack.isEmpty() && stack.peek() == popped[i]){
                    stack.pop();
                    i++;
                }
            }
    
            return stack.isEmpty();//判断栈是否为空
        }
    }
    
    0 赞同 0 条回复
  • Luminous 普通 2023年7月11日 下午8:09
    class Solution {
        public boolean validateStackSequences(int[] pushed, int[] popped) {
            if(pushed==null||pushed.length<=0){
                return true;
            }
            int k=0;
            Stack<Integer> stack=new Stack<Integer>();
            for(int i=0;i<pushed.length;i++){
                stack.push(pushed[i]);
                while(!stack.isEmpty()&&stack.peek().intValue()==popped[k]){
                    stack.pop();
                    k++;
                }
            }
            return stack.isEmpty();
        }
    }
    

    时间复杂度:O(N)
    空间复杂度: O(N)

    0 赞同 0 条回复
  • V2ero 普通 2023年7月11日 下午7:05

    不看题解根本想不到

    function validateStackSequences(pushed: number[], popped: number[]): boolean {
        if(pushed === null || !pushed.length){
            return true
        }
        let k = 0;
        const stack = [];
        for(let i = 0 ; i < pushed.length; i += 1){
            stack.push(pushed[i])
            while(stack.length && stack[stack.length - 1] === popped[k]){
                stack.pop();
                k += 1
            }
        }
    
        return !!!stack.length;
    };
    
    0 赞同 0 条回复
  • CKX 普通 2023年7月11日 下午5:09
    func validateStackSequences(pushed []int, popped []int) bool {
        stack := []int{}
        //popped下标
        i := 0
        for _, x := range pushed {
            //将pushed数组内容入栈
            stack = append(stack, x)
            for len(stack) > 0 && stack[len(stack) - 1] == popped[i] {
                stack = stack[:len(stack)-1]
                i++
            }
        }
    
        return len(stack) == 0
    }
    
    • 时间O(n)
    • 空间O(n)
    0 赞同 0 条回复
  • mpweixin用户 普通 2023年7月11日 下午4:40
    public boolean validateStackSequences(int[] pushed, int[] popped) {
            // 输入:pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
            //输出:true
            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();
    
        }
    
    0 赞同 0 条回复
  • 波雅汉库克 普通 2023年7月11日 下午4:20
    class Solution {
    public:
        bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
            if(pushed.size()!=popped.size()){
                return false;
            }
            stack<int> stk;
            int j = 0;
            for(int i=0; i<pushed.size(); i++){
                stk.push(pushed[i]);
                while(!stk.empty() && j<popped.size() &&stk.top() == popped[j]){
                    stk.pop();
                    j++;
                }
            }
            if(!stk.empty() || j < popped.size()){
                return false;
            }
            return true;
        }
    };
    

    时间复杂度:O(n)
    空间复杂度:O(n)

    0 赞同 0 条回复
  • 桎梏 普通 2023年7月11日 下午3:55
    class Solution {
        public boolean validateStackSequences(int[] pushed, int[] popped) {
    
            Stack<Integer> stack = new Stack<>();
            int cur = 0;
            for(int i = 0; i < pushed.length; i++) {
                stack.push(pushed[i]);
                while (!stack.isEmpty() && stack.peek().equals(popped[cur])) {
                    stack.pop();
                    cur++;
                }
            }
            return stack.isEmpty(); 
    
        }
    }
    

    时间复杂度:O(n)
    空间复杂度:O(n)

    0 赞同 0 条回复
  • Mooby 普通 2023年7月11日 下午3:42

    五
    class Solution {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
    if (pushed null || pushed.length <= 0) {
    return true;
    }
    int k = 0;
    Stack stack = new Stack();
    for (int i = 0; i < pushed.length; i++) {
    stack.push(pushed[i]);
    while (!stack.isEmpty() && stack.peek()
    popped[k]) {
    stack.pop();
    k++;
    }
    }
    return stack.isEmpty();
    }
    }

    0 赞同 0 条回复
  • 小季同学 普通 2023年7月11日 下午3:30

    画图更好理解点

    function validateStackSequences(pushed: number[], popped: number[]): boolean {
        // 这两个序列分别是入栈序列和出栈序列,一个指针指向入栈,一个指向出栈,相同则出栈,poped+1,不同则继续入栈遍历,入栈
        // 边界判断 
        if(pushed === null || pushed.length <= 0) return true
        const stack: number[] = []
        let k = 0
        // 入栈顺序依次入栈
        for(let i = 0; i < pushed.length; i++) {
            stack.push(pushed[i])
            // 每次判断入栈的栈顶元素是否与出栈顺序相等
            while(stack.length > 0 && stack[stack.length - 1] === popped[k]) {
                // 出栈
                stack.pop()
                k++
            }
        }
        return stack.length === 0 ? true : false
    
    };
    
    0 赞同 0 条回复

# 回答此问题

后才能回答