代码:
package targetOffer;
import java.util.*;
public class Stack2_22 {
boolean isPopOrder(Queue<Integer> pPush,Queue<Integer>pPop){
boolean possible=false;
if(pPush!=null&&pPop!=null)
{
Iterator<Integer>it1= pPush.iterator();
Iterator<Integer>it2= pPop.iterator();
Stack<Integer> stackData=new Stack<Integer>();
while(it2.hasNext()){
int temp=it2.next();
while(stackData.isEmpty()||stackData.peek()!=temp)
{
if(it1.hasNext()==false)
{
break;
}
stackData.push(it1.next());
}
if(stackData.peek()!=temp)
break;
stackData.pop();
}
if(stackData.isEmpty()&&!it2.hasNext())
possible=true;
}
return possible;
}
void test()
{
Queue<Integer> push=new LinkedList<Integer>();
Queue<Integer> pop1=new LinkedList<Integer>();
for (int i=1;i<6;i++)
push.offer(i);
pop1.offer(4);
pop1.offer(3);
pop1.offer(5);
pop1.offer(1);
pop1.offer(2);
boolean result= isPopOrder(push,pop1);
System.out.println(result);
}
public static void main(String args[])
{
Stack2_22 example=new Stack2_22();
example.test();
}
}
结果:
false