首页 > 其他 > 详细

给定入栈序列,判断一个串是否为出栈序列

时间:2016-04-22 16:29:50      阅读:187      评论:0      收藏:0      [点我收藏+]
剑指offer22:给定入栈序列,判断一个串是否为出栈序列
public static boolean isOutStackSequence(int[] Spush, int[] Spop) {
        if (Spush.length <= 0 || Spop.length <= 0 || Spush.length != Spop.length)
            return false;
        int len = Spush.length;
        Stack<Integer> s = new Stack<Integer>();
        int i=0,j=0;
        for (; i < len; i++) {
            while(s.isEmpty()||Spop[i]!=s.peek()){
                if(j<len){
                    s.push(Spush[j]);
                    j++;
                }else{
                    break;
                }
            }
            if(Spop[i]!=s.peek()) break;
                s.pop();
        }
        if(i<len)return false;
        else return true;
    }

 


给定入栈序列,判断一个串是否为出栈序列

原文:http://www.cnblogs.com/todayjust/p/5421585.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!