栈的push、pop 序列
来源:互联网 发布:上海峰途网络 编辑:程序博客网 时间:2024/06/05 16:31
题目:输入两个整数序列。其中一个序列表示栈的push 顺序,
判断另一个序列有没有可能是对应的pop 顺序。
为了简单起见,我们假设push 序列的任意两个整数都是不相等的。
比如输入的push 序列是1、2、3、4、5,那么4、5、3、2、1 就有可能是一个pop 系列。
因为可以有如下的push 和pop 序列:
push 1,push 2,push 3,push 4,pop,push 5,pop,pop,pop,pop,
这样得到的pop 序列就是4、5、3、2、1。
但序列4、3、5、1、2 就不可能是push 序列1、2、3、4、5 的pop 序列。
判断另一个序列有没有可能是对应的pop 顺序。
为了简单起见,我们假设push 序列的任意两个整数都是不相等的。
比如输入的push 序列是1、2、3、4、5,那么4、5、3、2、1 就有可能是一个pop 系列。
因为可以有如下的push 和pop 序列:
push 1,push 2,push 3,push 4,pop,push 5,pop,pop,pop,pop,
这样得到的pop 序列就是4、5、3、2、1。
但序列4、3、5、1、2 就不可能是push 序列1、2、3、4、5 的pop 序列。
bool fun(int a[], int b[], int n){int i = 0;int j = 0;stack<int> buf;while (i < n && j < n){if (a[i] == b[j]){i++;j++;}else{buf.push(a[i]);i++;}}while (!buf.empty()){int top = buf.top();if (top != b[j]){return false;}else{buf.pop();j++;}}return true;}
0 0
- 栈的push、pop序列
- 栈的push,pop序列
- 栈的push、pop序列
- 栈的push、pop序列
- 栈的push、pop序列
- 栈的push、pop序列
- 栈的push、pop序列
- 栈的push、pop序列
- 栈的push、pop序列
- 栈的push、pop序列
- 栈的push、pop序列
- 栈的push、pop序列
- 栈的push、pop序列
- 栈的push,pop序列
- 栈的push、pop序列
- 栈的push、pop序列
- 栈的push、pop序列
- 栈的push、pop 序列
- vue指令的定义及常用指令
- 软件历史知识
- 根据时间生成唯一ID
- 虚拟机通过NAT上外网
- bnrfjxtdlrjxdrp
- 栈的push、pop 序列
- Vue-click事件
- 交通部道路运输车辆卫星定位系统部标JTT808、809、796标准大全
- 一条基本的python + appium测试用例
- wxpython类似TK中的label frame实现
- fjrpjhhxrvrrjjt
- vue的单击隐藏、双击出现事件:
- 关于JAVA类的反射理解
- VUE冒泡及阻止冒泡