您的位置:首页 > 其它

输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一

2017-09-06 10:57 911 查看
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
if( pushA.length != popA.length || pushA.length<=0 ||popA.length<= 0){
return false;
}
boolean res = false;
Stack<Integer> st = new Stack<Integer>();
int popIndex = 0;
for(int i = 0 ; i < pushA.length ; i++){
st.push(pushA[i]);
if( st.peek() != popA[popIndex])
continue;
else{
st.pop();
popIndex++;
}
}
while( !st.isEmpty() ){
if(st.peek() == popA[popIndex]){
st.pop();
popIndex++;
}
else
break;
}
if( st.isEmpty() && popIndex == popA.length )
res = true;
return res;

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐