0
点赞
收藏
分享

微信扫一扫

判断元素出栈的合法性

金牛豆豆 2022-03-18 阅读 75

 

package stack;

import java.util.Stack;

public class StaticShun {
    public boolean hhh(int[] pushed,int[] poped){
        Stack<Integer> stack = new Stack<>();
        int i = 0;
        for (int num:pushed){
            stack.push(num);
            while (!stack.isEmpty()&&stack.peek()==poped[i]){
                stack.pop();
                i++;
            }
        }
        return stack.isEmpty();
    }
}
举报

相关推荐

0 条评论