zoukankan      html  css  js  c++  java
  • 946. 验证栈序列

    模拟

    class Solution {
        public boolean validateStackSequences(int[] pushed, int[] popped) {
            int pushNum = pushed.length;
            int poped = popped.length;
            int indexPush = 0;
            int indexPop =0;
            Stack<Integer> stack = new Stack<>();
            boolean flag = true;
            while(true){
                if(indexPop==poped) break;
                if(stack.size()==0) {
                    stack.push(pushed[indexPush]);
                    indexPush++;
                }
                if(stack.peek()==popped[indexPop]){
                    stack.pop();
                    indexPop++;
                }
                if(stack.size()!=0 && stack.peek()!=popped[indexPop] && indexPush<pushNum){
                    stack.push(pushed[indexPush]);
                    indexPush++;
                }
                if( stack.size()!=0 && stack.peek()!=popped[indexPop] && indexPush==pushNum){
                    flag = false;
                    break;
                }
            }
    
            return flag;
    
        }
    }
    作者:你的雷哥
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    python之private variable
    python实例、类方法、静态方法
    python常用option
    access
    FD_CLOEXEC
    fork后父子进程文件描述问题
    split
    信号
    kill
    进程组&Session
  • 原文地址:https://www.cnblogs.com/henuliulei/p/15354861.html
Copyright © 2011-2022 走看看