思路,用额外的一个栈去存储pushA中的元素,然后按照popA的弹出顺序。
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
if(pushA.length==0||popA.length==0)
return false;
Stack<Integer> stack = new Stack();
//1 放入puchA的第一个元素
int indexPush = 0;
int indexPop = 0;
stack.push(pushA[indexPush++]);
while(indexPop<popA.length){
if(popA[indexPop]!=stack.peek()){
if(indexPush<pushA.length)
stack.push(pushA[indexPush++]);
else
return false;
}
if(popA[indexPop]==stack.peek()){
stack.pop();
indexPop++;
}
}
return stack.isEmpty();
}
}
版权声明:本文为weixin_35364049原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。