题目:输入两个整数序列。其中一表示栈的 push 顺序,判断另一个序列有没可能是对应的 pop 顺序
package search; import java.util.Stack; public class PushPopSeries { public static boolean isPopSeries(int[] push,int[] pop){ if(push.length!=pop.length){ return false; } int i1=0,i2=0,len=pop.length; Stack<Integer> stack=new Stack<Integer>(); while(i2<len){ while(stack.isEmpty()||stack.peek()!=pop[i2]){ if(i1<len){ stack.push(push[i1++]); }else if(!stack.isEmpty()){ return false; }else{ break; } while(!stack.isEmpty()&&stack.peek()==pop[i2]){ stack.pop(); i2++; } } } return true; } public static void main(String args[]){ int[] push={1,2,3,4,5}; int[] pop={4,5,3,2,1}; int[] pop2={4,3,5,1,2}; System.out.println(PushPopSeries.isPopSeries(push, pop)); System.out.println(PushPopSeries.isPopSeries(push, pop2)); } }