判断栈的出栈和入栈序列O(N)时间O(N)空间
2012-09-20 18:31
148 查看
给出一个序列式栈的入栈序列,但是在入栈的过程中可能会有元素出栈,问最后的出栈序列是否可能出现
,思路就是你想的那样。不再赘述,实在打字麻烦,看代码页很容易看的。就不写了。
,思路就是你想的那样。不再赘述,实在打字麻烦,看代码页很容易看的。就不写了。
void init_queue(queue<int>& q , const int a[],const int n) { for (unsigned i=0;i<n;i++) { q.push(a[i]); } } void check_stack_order(int pushdata[],int checkdata[],int n) { queue<int> pushqueue; init_queue(pushqueue,pushdata,n); stack<int> tempstack; int i=0; while (true) { if (!tempstack.empty() && tempstack.top()==checkdata[i]) { tempstack.pop(); ++i; } else { if (pushqueue.empty()) { break; } tempstack.push(pushqueue.front()); pushqueue.pop(); } } if (i==n) { cout<<"wright order !"<<endl; } else { cout<<"wrong order !"<<endl; } } int main( void ) { int pushdata[]={1,2,3,4,5}; //int check[]={5,1,2,4,3}; int check[]={2,1,4,3,5}; check_stack_order(pushdata,check,sizeof(pushdata)/sizeof(int)); return 0; }
相关文章推荐
- java 判断两个字符串是否由相同的字符组成 排序算法 空间换时间的方法
- 时间序列分析——如何判断序列是否平稳
- 衡量视频序列特性的TI(时间信息)和SI(空间信息)
- 每天一道LeetCode-----找到序列中第一个没有出现的正整数,要求时间复杂度是O(n),空间复杂度是O(1)
- 时间序列分析(一) 如何判断序列是否平稳
- PHP判断素数,循环,筛选——默认可执行时间秒数,默认可执行空间。用来测试运行效率
- 模拟赛 序列问题(时间限制:1s;空间限制:128MB)
- 时间序列分析(一) 如何判断序列是否平稳
- 如何判断时间序列AR(p)和MA(q)模型
- 视频序列的空间复杂度和时间复杂度的不用模型计算
- 经典算法 | 判断任意长度链表是否有环的O(n)时间,O(1)空间算法
- 判断序列中是否存在两个元素之和为x,时间复杂度O(nlgn),算法导论练习2.3,linux纯C实现
- 时间序列 R 读书笔记 05 判断预测法 Judgmental forecasts
- PostgreSQL中的大容量空间探索时间序列数据存储
- Android之判断时间是否为今天
- 图结构练习——判断给定图是否存在合法拓扑序列
- 基于时间序列的用户行为预测
- 为物联网而生:高性能时间序列数据库HiTSDB商业化首发!
- 二叉搜索树后序遍历序列判断
- Havel-Hakimi定理(判断一个序列是否可图)->POJ1659