21. 栈的压入、弹出序列
新建一个栈,将数组A压入栈中,当栈顶元素等于数组B时,就将其出栈,当循环结束时,判断栈是否为空,若为空则返回true.
1 import java.util.Stack; 2 public class Solution { 3 // 4 public boolean IsPopOrder(int [] pushA,int [] popA) { 5 if(pushA.length == 0 || popA.length == 0) 6 return false; 7 Stack<Integer> stack = new Stack<>(); 8 int j = 0; 9 for(int i = 0; i < pushA.length; i++){ 10 stack.push(pushA[i]); 11 while(!stack.empty() && stack.peek() == popA[j]){ 12 stack.pop(); 13 j++; 14 } 15 } 16 return stack.empty(); 17 } 18 }
原文:https://www.cnblogs.com/hi3254014978/p/12357349.html