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

    题目描述

    输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
     
     1 import java.util.ArrayList;
     2 
     3 import java.util.Stack;
     4 
     5 public class Solution {
     6     public boolean IsPopOrder(int [] pushA,int [] popA) {
     7       if(pushA==null||popA==null)
     8               return false;
     9           Stack<Integer> stack=new Stack<>();
    10           int index=0;
    11           for(int i=0;i<pushA.length;i++) {
    12               stack.push(pushA[i]);
    13               while(!stack.isEmpty()&&stack.peek()==popA[index]) {
    14                   stack.pop();
    15                   index++;
    16               }
    17           }
    18           return stack.isEmpty();
    19     }
    20 }
  • 相关阅读:
    Sky
    MyEclipse 10中文汉化教程
    算法
    查找众数
    格雷码算法
    commons-email
    java
    IO端寻址
    存储器
    汇编顺序程序设计
  • 原文地址:https://www.cnblogs.com/jacob-wuhan/p/12979778.html
Copyright © 2011-2022 走看看