当前位置:网站首页>946. 验证栈序列
946. 验证栈序列
2022-07-05 12:39:00 【厚积薄发ض】
给定 pushed 和 popped 两个序列,每个序列中的 值都不重复,只有当它们可能是在最初空栈上进行的推入 push 和弹出 pop 操作序列的结果时,返回 true;否则,返回 false 。
示例 1:
输入:pushed = [1,2,3,4,5], popped = [4,5,3,2,1] 输出:true 解释:我们可以按以下顺序执行: push(1), push(2), push(3), push(4), pop() -> 4, push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
示例 2:
输入:pushed = [1,2,3,4,5], popped = [4,3,5,1,2] 输出:false 解释:1 不能在 2 之前弹出。
提示:
1 <= pushed.length <= 10000 <= pushed[i] <= 1000pushed的所有元素 互不相同popped.length == pushed.lengthpopped是pushed的一个排列
我们做法是先将进栈序列入栈,当出栈序列与入栈序列相同的时候,栈顶元素要出栈,同时出栈序列接着遍历下一个元素
这是相同的情况:

这是不同的情况:

思路:
让入栈队列一一入栈,看栈顶元素是否与出栈队列元素相同,如果不相同,依次让入栈队列一一入栈,如果相同就让栈顶元素出栈,同时出栈队列遍历下一个元素,继续往复循环,最后如果入栈数组遍历完成并且栈为空就证明入栈队列与出栈队列是相同的,如果最后入栈数组遍历完成而栈不为空,那就证明入栈队列与出栈队列不相同。
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
Stack<Integer> s = new Stack<Integer>();
int index = 0;
for(int i = 0 ; i < pushed.length; i ++){
s.push(pushed[i]);//将入栈序列压栈
while(!s.isEmpty() && index<popped.length&&popped[index] == s.peek()){
//在栈不为空的情况下并且index不越界看出栈序列是否与栈顶元素相同
s.pop();//如果相同弹出栈顶元素
index++;//遍历下一个元素
}
}
return s.isEmpty();
}
}边栏推荐
- Pytoch uses torchnet Classerrormeter in meter
- Comprehensive upgrade of Taobao short video photosynthetic platform
- Iterator details in list... Interview pits
- RHCSA2
- Simply take stock reading notes (1/8)
- I met Tencent in the morning and took out 38K, which showed me the basic smallpox
- Redis cluster configuration
- 使用 jMeter 对 SAP Spartacus 进行并发性能测试
- Transactions on December 23, 2021
- [cloud native] use of Nacos taskmanager task management
猜你喜欢

Distance measuring sensor chip 4530a used in home intelligent lighting

Preliminary exploration of basic knowledge of MySQL

Storage Basics

自然语言处理系列(一)入门概述

RHCSA3

Annotation problem and hidden Markov model

初识Linkerd项目

Four common problems of e-commerce sellers' refund and cash return, with solutions

Principle of universal gbase high availability synchronization tool in Nanjing University

Introduction to the principle of DNS
随机推荐
Database connection pool & jdbctemplate
insmod 提示 Invalid module format
Distributed cache architecture - cache avalanche & penetration & hit rate
Redis cluster configuration
Storage Basics
The relationship between the size change of characteristic graph and various parameters before and after DL convolution operation
Laravel文档阅读笔记-mews/captcha的使用(验证码功能)
自然语言处理从小白到精通(四):用机器学习做中文邮件内容分类
SAP UI5 FlexibleColumnLayout 控件介绍
Shi Zhenzhen's 2021 summary and 2022 outlook | colorful eggs at the end of the article
Full text search of MySQL
Add a new cloud disk to Huawei virtual machine
Simply take stock reading notes (4/8)
SAP 自开发记录用户登录日志等信息
Reshape the power of multi cloud products with VMware innovation
Preliminary exploration of basic knowledge of MySQL
JDBC exercise - query data encapsulated into object return & simple login demo
SAP self-development records user login logs and other information
HiEngine:可媲美本地的云原生内存数据库引擎
ActiveMQ installation and deployment simple configuration (personal test)