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

来源:互联网 发布:除了淘宝客还有什么 编辑:程序博客网 时间:2024/05/16 18:40
public class Solution {
    public boolean IsPopOrder(int [] pushA,int [] popA) {
        int len = pushA.length;
        Stack<Integer> s = new Stack();
        int index =0;
        for(int i=0;i<len ;i++){
            if(pushA[i] == popA[index]){
                if(index == len-1 ) return true;
                index++;
            }else{
                s.push(pushA[i]);
            }
        }
        
        while(!s.isEmpty()){
            int a = s.pop();
            if(a == popA[index]){
                if(index == len-1 ) return true;
                index++;
            }else{
                return false;
            }
        }
        return false;
    }
}
阅读全文
0 0
原创粉丝点击