当前位置:网站首页>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();
}
};
边栏推荐
- Mysql—主从复制
- 为什么使用opengaussjdbc的时候老是出现FATAL?(标签-数据库|关键词-user)
- Software testing weekly (No. 81): what can resist negativity is not positivity, but concentration; What can resist anxiety is not comfort, but concrete.
- Details of task switching
- redis 哨兵模式
- Bubble sort - just read one
- VRRP+MSTP配置详解【华为eNSP实验】
- Learning summary of ugly code
- Without Huawei, Qualcomm will raise prices at will, and domestic mobile phones that lack core technology can only be slaughtered
- 2022 the most NB JVM foundation to tuning notes, thoroughly understand Alibaba P6 small case
猜你喜欢

VMWARE平台STS证书过期

MySQL string sorted by numeric value

After effects tutorial, how to create animation in after effects?

满足多种按键的高性价比、高抗干扰触摸IC:VK3606D、VK3610I、VK3618I 具有高电源电压抑制比

MySQL-字符串按照数值排序

Vinka introduces high anti-interference vk36n series touch IC: vk36n1d, vk36n2p, vk36n3b, vk36n4i, easy to use

關於初始化page入參的設計思路

激光共聚焦如何选择荧光染料

(Zset) how is the underlying layer of redis stored with a hop table

Summary of server performance tuning experience
随机推荐
远程系统命令执行
Redis sentinel mode
W3C 推出去中心化标识符作为 Web 标准
VRRP+MSTP配置详解【华为eNSP实验】
MySQL - master-slave replication
TranslucentTB 推荐
Esp8266 nodemcu flash file system (spiffs)
牛客-TOP101-BM36
ICML 2022 | 稀疏双下降:网络剪枝也能加剧模型过拟合?
Google Earth engine -- null value problem in image statistics
虚拟主播、偶像代言产品出问题谁负责?律师解析
lc marathon 7.23
After effects tutorial, how to create animation in after effects?
First hello of SOC_ World experiment
GO语言学习——复习包、接口、文件操作
lc marathon 7.23
快递单证智能OCR识别,助力物流行业数字化升级
Reproduce various counter attack methods
V自P建N_部署使用
Umijs - data transmission between main and sub applications of Qiankun