剑指offer(21)栈的压入弹出

package OfferTest;
//栈的压入,弹出序列
import java.util.Stack;

public class NoTwentyone {
    
    public static boolean IsPopOrder(int[] pushA,int[] popA){
        if(pushA == null || popA == null){
            return false;
        }
        Stack<Integer> stack = new Stack<Integer>();
        int curPop = 0;
        for(int i = 0;i < pushA.length;i++){
            stack.push(pushA[i]);
            while(!stack.isEmpty() && stack.peek() == popA[curPop]){
                stack.pop();
                curPop++;
            }
        }
        return stack.isEmpty();
    }
    
    public static void main(String[] args){
        int[] pushA = new int[]{1,2,3,4,5};
        int[] popA = new int[]{4,5,3,2,1};
        boolean res = IsPopOrder(pushA,popA);
        System.out.println(res);
        
    }

}
 

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-02 18:35
简历上把1个月实习写成了3个月,会进行背调吗?
码农索隆:一个月有一个月的实习经历,三个月有三个月的实习经历
点赞 评论 收藏
分享
点赞 评论 收藏
分享
能干的三文鱼刷了10...:公司可能有弄嵌入式需要会画pcb的需求,而且pcb能快速直观看出一个人某方面的实力。看看是否有面试资格。问你问题也能ai出来,pcb这东西能作假概率不高
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务