您的位置:首页 > 运维架构

输入两个整数序列。其中一个序列表示栈的push顺序,判断另一个序列有没有可能是对应的pop顺序。为了简单起见,我们假设push序列的任意两个整数都是不相等的。 比如输入的push序列是1、2、3、4、5,那么4、5、3、2、1就有可能是一个pop系列。

2013-05-13 15:57 966 查看
bool isPopOrder(int pushA[], int popA[], int length) {
stack<int> st;
int i = 0, j = 0;
while(i < length && j < length) {
st.push(pushA[i++]);
while(!st.empty() && popA[j] == st.top()) {
st.pop();
j++;
}
}
return (st.empty() && j == length);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐