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;

    }
}

 

相关文章:

  • 2022-02-06
  • 2022-12-23
  • 2021-10-26
  • 2021-06-20
  • 2022-12-23
  • 2022-12-23
  • 2021-09-23
猜你喜欢
  • 2022-12-23
  • 2021-12-30
  • 2022-02-23
  • 2021-10-16
  • 2021-06-05
  • 2021-09-24
相关资源
相似解决方案