当前位置:网站首页>946. Verify stack sequence ●● & sword finger offer 31. stack push in and pop-up sequence ●●
946. Verify stack sequence ●● & sword finger offer 31. stack push in and pop-up sequence ●●
2022-07-23 16:20:00 【chenyfan_】
946. Verify stack sequence ●● & The finger of the sword Offer 31. Pressure into the stack 、 Pop-up sequence ●●
describe
Given pushed and popped Two sequences , In each sequence The values are not repeated , Only if they could have been pushed on the original empty stack push And pop up pop When manipulating the result of a sequence , return true; otherwise , return false .
Example
Input : pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
Output : true
explain : We can do it in the following order :
push(1), push(2), push(3), push(4), pop() -> 4,
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
Input :pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
Output :false
explain :1 Can't be in 2 Pop up before .
Answer key
1. simulation
All elements must be in order push In the , What matters is how pop come out .
Because the element values are not repeated , So you can pushed Every number in the queue push To a stack , At the same time, check whether this number is popped The next in the sequence is pop Value , If so, put it pop come out .
Last , Check whether all elements are pop Come out , namely Is the stack empty .
- Time complexity :O(N), among N yes pushed Sequence sum popped The length of the sequence .
- Spatial complexity :O(N).


class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
stack<int> st;
int idx = 0;
for(int num : pushed){
st.push(num);
// Loop to check whether the current stack top element is about to pop The element value of
while(!st.empty() && st.top() == popped[idx] && idx < pushed.size()){
st.pop();
++idx; // want pop The value of is shifted back
}
}
return st.empty();
}
};
边栏推荐
- Kubernetes 基本概念和部署
- Dark horse programmer - interface test - four day learning interface test - third day - advanced usage of postman, export and import of Newman case set, common assertions, assertion JSON data, working
- es6把多个class方法合并在一起
- CA数字证书
- Comparison of functional characteristics and parameters of several solar panel battery charging management ICs cs5363, cs5350 and cs5328
- Squid 代理服务之透明代理服务器架构搭建
- table自定义表格的封装
- 黑马程序员-接口测试-四天学习接口测试-第三天-postman高级用法,newman例集导出导入,常用断言,断言json数据,工作原理,全局,环境变量,时间戳,请求前置脚本,关联,批量执行测试用例
- ORA-01654错误:表空间满了,插入失败
- ECS remote monitoring
猜你喜欢

冒泡排序-看着一篇就够啦

sqlnet.ora文件设置不对造成ORA-12154 、ORA-01017连接异常
![[suctf 2018]multisql (MySQL precompiled)](/img/ae/501b7f9c6d8259c3c799e4ff0b568b.png)
[suctf 2018]multisql (MySQL precompiled)

SharedPreferences数据储存

Another award | opensca was selected as the "top ten open source software products in the world" at the China Software Expo

sqlnet. Ora-12154 and ora-01017 connection exceptions caused by incorrect ora file settings

After effects tutorial, how to create animation in after effects?
CA数字证书

Cloud native (11) | kubernetes chapter kubernetes principle and installation

Comparison of functional characteristics and parameters of several solar panel battery charging management ICs cs5363, cs5350 and cs5328
随机推荐
WSAStartup函数的用途
Backup content hahaha
From the big guy baptism! 2022 headline first hand play MySQL advanced notes, and it is expected to penetrate P7
pgsql误删除pg_wal文件后,服务启动失败
FPGA-HLS-乘法器(流水线对比普通仿真)
MySQL-字符串按照数值排序
After Effects 教程,如何在 After Effects 中创建动画?
牛客-TOP101-BM35
Alamofire 框架封装与使用
7、 Logic of JMeter sending request
redis 安装
ECS remote monitoring
[suctf 2018]multisql (MySQL precompiled)
Kubernetes 基本概念和部署
SOC的第一个Hello_World实验
vulnstack红日-4
封片剂 甘油封片 中性树脂封片剂型
redis 主从复制
Class homework (5) -- 576. Hungry cattle
VMWARE平台STS证书过期