一个序列是否可能是push序列的pop序列
2014-09-27 00:15
183 查看
输入:两个序列A、B,A作栈的push序列,判断B是否可能是A的pop序列。
#include<iostream> #include<stack> using namespace std; bool solution(const int* apush, const int* apop, int len){ if(len < 1) return false; int i = 0, j = 0; stack<int> stack; while(j < len || !stack.empty()){ if(i < len){ if(apush[i] != apop[j]) stack.push(apush[i++]); else i++, j++; } while(!stack.empty() && stack.top() == apop[j]) stack.pop(), j++; if(i == len && !stack.empty()) break; } return stack.empty() && j == len; } int main(){ int aPush[5] = {1, 2, 3, 4, 5}; int aPop[5] = {4, 3, 5, 2, 1}; cout<<solution(aPush, aPop, 5); return 0; } //output: 1
相关文章推荐
- 一个序列是否可能是push序列的pop序列
- 第29题:推断一个序列是否是还有一个push序列的pop序列
- 两个整数序列,其中一个序列表示栈的push顺序, 判断另一个序列有没有可能是对应的pop顺序。
- 输入两个整数序列。其中一个序列表示栈的push顺序, 判断另一个序列有没有可能是对应的pop顺序。 为了简单起见,我们假设push序列的任意两个整数都是不相等的
- 【算法】输入两个整数序列。其中一个序列表示栈的push顺序,判断另一个序列有没有可能是对应的pop顺序。
- 题目:输入两个整数序列。其中一个序列表示栈的push顺序,判断另一个序列有没有可能是对应的pop顺序。
- 输入两个整数序列。其中一个序列表示栈的push顺序,判断另一个序列有没有可能是对应的pop顺序
- 每天学习一算法系列(27)(输入两个整数序列。其中一个序列表示栈的push 顺序,判断另一个序列有没有可能是对应的pop 顺序)
- <仅是自己做笔记。。。系列-14>输入两个整数序列。其中一个序列表示栈的push顺序, 判断另一个序列有没有可能是对应的pop顺序。
- 【练习】输入两个整数序列。其中一个序列表示栈的push 顺序,判断另一个序列有没有可能是对应的pop 顺序
- 输入两个整数序列。其中一个序列表示栈的push顺序, 判断另一个序列有没有可能是对应的pop顺序。 为了简单起见,我们假设push序列的任意两个整数都是不相等的
- 微软算法100道题-------输入两个整数序列。其中一个序列表示栈的push顺序, 判断另一个序列有没有可能是对应的pop顺序。
- 第29题:判断一个序列是否是另一个push序列的pop序列
- 谈谈如何判断一个Pop序列是否是一个Push序列的Pop顺序
- 输入两个整数序列。其中一个序列表示栈的push顺序,判断另一个序列有没有可能是对应的pop顺序。为了简单起见,我们假设push序列的任意两个整数都是不相等的。 比如输入的push序列是1、2、3、4、5,那么4、5、3、2、1就有可能是一个pop系列。
- PHP 输入两个整数序列。其中一个序列表示栈的push 顺序, 判断另一个序列有没有可能是对应的pop 顺序
- 算法习题29:栈的push、pop序列是否一致
- 笔试题:判断栈的push(),pop()序列是否正确
- Java 判断栈的push和pop序列是否一致
- (栈)栈 给定push序列,判断给定序列是否是pop序列