在线编程--栈的压入、弹出序列

来源:互联网 发布:智能开关 app 源码 编辑:程序博客网 时间:2024/06/05 08:17

题目描述

输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。

import java.util.ArrayList;import java.util.*;public class Solution {    public boolean IsPopOrder(int [] pushA,int [] popA) {        if(pushA==null || pushA.length==0){            return false;        }        if(popA==null || popA.length==0){            return false;        }        Stack<Integer> stackPop=new Stack<Integer>();        stackPop.push(0);        for(int i=popA.length-1;i>=0;i--){            stackPop.push(popA[i]);        }        Stack<Integer> stackPush=new Stack<Integer>();        stackPush.push(-1);        //while(stackPop.peek()!=null){                       for(int j=0;j<pushA.length;j++){                stackPush.push(pushA[j]);                while(stackPop.peek()==stackPush.peek()){                    stackPop.pop();                    stackPush.pop();                }               }            if(stackPop.peek()==0&&stackPush.peek()==-1){                return true;            }            else{                return false;            }        //}    }}
0 0
原创粉丝点击