import java.util.Stack;
public class isStackOrder {
public static boolean isOrder(final int[] push,final int[] pop){
if(null==push&&null==pop)
return true;
else if(null==push||null==pop)
return false;
else if(push.length!=pop.length)
return false;
int cpush=0,cpop=0;
Stack<Integer> st = new Stack<Integer>();
while(cpush<push.length){
if(st.size()<=0)
st.push(push[cpush++]);
else if(st.peek()!=pop[cpop])
st.push(push[cpush++]);
else{
st.pop();
cpop++;
}
}
while(!st.isEmpty()){
if(cpop>=pop.length || st.pop()!=pop[cpop++]){
return false;
}
}
return true;
}
public static void main(String[] argvs){
System.out.println(isOrder(null,null));
System.out.println(isOrder(null,new int[] {1}));
System.out.println(isOrder(new int[] {},new int[] {1}));
System.out.println(isOrder(new int[] {1},new int[] {1}));
System.out.println(isOrder(new int[] {1,2,3,4,5},new int[] {1,2,3,4,5}));
System.out.println(isOrder(new int[] {1,2,3,4,5},new int[] {4,5,3,2,1}));
}
}
判断序列是否为合法出栈
最新推荐文章于 2023-09-15 20:09:12 发布