栈的压入、弹出序列
来源:互联网 发布:工具书阅读软件 编辑:程序博客网 时间:2024/06/06 19:08
title:
栈的压入、弹出序列:输入两个整数序列,第一个是压入序列,判断第二个是不是栈的弹出序列
thought:
利用一个辅助栈:按照输入的第二个序列,将第一个序列的数据依次压入栈
判断一个序列是不是栈的弹出序列的规律:如果下一个弹出的数据(由第二个序列决定)刚好是栈顶元素,则直接弹出;
如果下一个弹出的数字不在栈顶,把压栈序列中还没有入栈的数字压入辅助栈,直到把下一个需要弹出的数字压入栈为止;
如果入栈序列的所有数据都入栈了仍然没有找到下一个弹出的数字,则该序列不可能是一个弹出序列。
bool IsPopOrder(const int p_push[],const int p_pop[],int length){bool Is_pop = false;if(p_push != NULL && p_pop != NULL && length > 0){ stack<int> popstack; const int *push_next = p_push; const int *pop_next = p_pop; while (pop_next-p_pop < length) //根据弹出序列来确定入栈的顺序 { while (popstack.empty() || popstack.top() != *pop_next) // 栈顶元素不等于弹出元素 { if(push_next-p_push == length) //到达入栈序列的尾部(即已经没有元素可以入栈) break; popstack.push(*push_next); //当辅助栈的栈顶元素不等于弹出序列元素时,将入栈序列中未入栈的元素压入(直至辅助栈的栈顶元素等于弹出元素或者到达入栈序列的尾部) push_next++; } if (popstack.top() != *pop_next) break; popstack.pop(); pop_next++; } if(popstack.empty() && pop_next-p_pop == length) Is_pop = true;}return Is_pop;}
0 0
- 1366栈的压入弹出序列
- 栈的压入、弹出序列
- 栈的压入、弹出序列
- 栈的压入、弹出序列
- Q22:栈的压入、弹出序列
- 栈的压入弹出序列
- 栈的压入、弹出序列
- 栈的压入弹出序列
- 栈的压入、弹出序列
- 栈的压入弹出序列
- 栈的压入、弹出序列
- 栈的压入、弹出序列
- 栈的压入弹出序列
- 栈的压入、弹出序列
- 栈的压入和弹出序列
- 栈的压入、弹出序列
- 栈的压入、弹出序列
- 栈的压入、弹出序列
- 黑马程序员--java基础--基础语法
- makefile 多目标和多规则
- UITableView
- JAVA版微信支付V3-完全版
- scala 第75讲:模式匹配下的For循环
- 栈的压入、弹出序列
- 触摸和摇一摇
- gcc简单使用
- 微信支付详解
- orcale 控制用户权限
- 第十四章约束
- 数据库模式的4个范式
- UVA 11748Rigging Elections 【dfs】
- 暑假集训第四周周六赛 A - Prime Ring Problem素数环