zoukankan      html  css  js  c++  java
  • 栈的push 、pop 序

    题目:输入两个整数序列。其中一表示栈的 push 顺序,判断另一个序列有没可能是对应的 pop 顺序

    package search;
    
    import java.util.Stack;
    
    public class PushPopSeries {
        public static boolean isPopSeries(int[] push,int[]  pop){
            if(push.length!=pop.length){
                return false;
            }
            int i1=0,i2=0,len=pop.length;
            Stack<Integer> stack=new Stack<Integer>();
            while(i2<len){
                while(stack.isEmpty()||stack.peek()!=pop[i2]){
                    if(i1<len){
                        stack.push(push[i1++]);
                    }else if(!stack.isEmpty()){
                        return false;
                    }else{
                        break;
                    }
                    while(!stack.isEmpty()&&stack.peek()==pop[i2]){
                        stack.pop();
                        i2++;
                    }
                }
            }
            return true;
        }
        public static void main(String args[]){
            int[] push={1,2,3,4,5};
            int[] pop={4,5,3,2,1};
            int[] pop2={4,3,5,1,2};
            System.out.println(PushPopSeries.isPopSeries(push, pop));
            System.out.println(PushPopSeries.isPopSeries(push, pop2));
        }
    }
  • 相关阅读:
    CSS
    表单
    框架
    表格
    列表
    定位--position属性
    浮动
    选择结构
    数组
    TextView(标签控件)
  • 原文地址:https://www.cnblogs.com/waka401/p/2710415.html
Copyright © 2011-2022 走看看