数据结构练习(19)栈的push、pop序列
2012-12-13 22:58
225 查看
http://zhedahht.blog.163.com/blog/static/25411174200732102055385/
很多细节操作在里面,还是比较经典的,需要反复磨练。空间复杂度O(n),时间复杂度O(n).
很多细节操作在里面,还是比较经典的,需要反复磨练。空间复杂度O(n),时间复杂度O(n).
#include <iostream> #include <stack> using namespace std; bool is_pop_order(const int *push, const int *pop, int len) { bool flag = false; if (push && pop && len > 0) { const int *nextpush = push; const int *nextpop = pop; std::stack<int> stdata; while (nextpop - pop < len) { while (stdata.empty() || stdata.top() != *nextpop) { if (!nextpush) break; stdata.push(*nextpush); if (nextpush - push < len - 1) ++nextpush; else nextpush = nullptr; } if (stdata.top() != *nextpop) break; stdata.pop(); ++nextpop; } if (stdata.empty() && nextpop - pop == len) flag = true; } return flag; } int main() { int a[10] = {1, 2, 3, 4, 5}; int b[10] = {4, 5, 3, 2, 1}; bool flag = is_pop_order(a, b, 5); if (flag) cout << "true" << endl; else cout << "false" << endl; return 0; }
相关文章推荐
- 程序员面试题精选100题(24)-栈的push、pop序列[数据结构]
- 程序员面试题精选100题(24)-栈的push、pop序列[数据结构]
- 程序员面试题精选100题(24)-栈的push、pop序列[数据结构]
- 栈的push、pop序列[数据结构]
- 【练习】输入两个整数序列。其中一个序列表示栈的push 顺序,判断另一个序列有没有可能是对应的pop 顺序
- 100题_24 栈的push、pop序列
- <仅是自己做笔记。。。系列-14>输入两个整数序列。其中一个序列表示栈的push顺序, 判断另一个序列有没有可能是对应的pop顺序。
- 输入两个整数序列。其中一个序列表示栈的push顺序, 判断另一个序列有没有可能是对应的pop顺序。 为了简单起见,我们假设push序列的任意两个整数都是不相等的
- 2-11. 两个有序链表序列的合并(15):链表数据结构基础练习
- PHP 输入两个整数序列。其中一个序列表示栈的push 顺序, 判断另一个序列有没有可能是对应的pop 顺序
- 程序员面试题100题第24题——判断是否是push、pop序列
- 根据栈的入栈序列和出栈序列,打印push和pop的顺序-Amazon笔试题
- 练习系列 - 3、设计包含min函数的栈,push、pop、min的时间复杂度都为O(1),仿照APR_RING采用宏实现
- 微软面试100题之29题:栈的push、pop 序列
- 【数据结构】堆的实现(包括:默认成员函数,插元素push,删元素pop,访问根节点top,判空,大小)
- 程序员面试题精选100题(24)-栈的push、pop序列
- 谈谈如何判断一个Pop序列是否是一个Push序列的Pop顺序
- 剑指Offer---面试题22:栈的push,pop序列问题
- 29.栈的push、pop序列(栈)
- 题目:输入两个整数序列。其中一表示栈的 push 顺序,判断另一个序列有没可能是对应的 pop 顺序