LeetCode 946. 验证栈序列(Validate Stack Sequences) 26
946. 验证栈序列
946. Validate Stack Sequences
题目描述
Given two sequences pushed and popped with distinct values, return true if and only if this could have been the result of a sequence of push and pop operations on an initially empty stack.
每日一算法2019/5/29Day 26LeetCode946. Validate Stack Sequences
Example 1:
Output: true
Explanation: We might do the following sequence:
push(1), push(2), push(3), push(4), pop() -> 4,
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
Example 2:
Output: false
Explanation: 1 cannot be popped before 2.
Note:
- 0 <= pushed.length == popped.length <= 1000
- 0 <= pushed[i], popped[i] < 1000
- pushed is a permutation of popped.
- pushed and popped have distinct values.
Java 实现
import java.util.Stack;
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
int n = pushed.length;
Stack<Integer> stack = new Stack<>();
for (int pushIndex = 0, popIndex = 0; pushIndex < n; pushIndex++) {
stack.push(pushed[pushIndex]);
while (popIndex < n && !stack.isEmpty() && stack.peek() == popped[popIndex]) {
stack.pop();
popIndex++;
}
}
return stack.isEmpty();
}
}
参考资料
- https://leetcode-cn.com/problems/validate-stack-sequences/
- https://leetcode.com/problems/validate-stack-sequences/
LeetCode 946. 验证栈序列(Validate Stack Sequences) 26的更多相关文章
- [Swift]LeetCode946. 验证栈序列 | Validate Stack Sequences
Given two sequences pushed and popped with distinct values, return true if and only if this could ha ...
- Leetcode 946. Validate Stack Sequences 验证栈序列
946. Validate Stack Sequences 题目描述 Given two sequences pushed and popped with distinct values, retur ...
- 946. Validate Stack Sequences
946. Validate Stack Sequences class Solution { public: bool validateStackSequences(vector<int> ...
- 【LeetCode】946. Validate Stack Sequences 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟过程 日期 题目地址:https://leetc ...
- 【leetcode】946. Validate Stack Sequences
题目如下: Given two sequences pushed and popped with distinct values, return true if and only if this co ...
- 112th LeetCode Weekly Contest Validate Stack Sequences
Given two sequences pushed and popped with distinct values, return true if and only if this could ha ...
- 946. Validate Stack Sequences验证栈序列
网址:https://leetcode.com/problems/validate-stack-sequences/ 参考:https://leetcode.com/problems/validate ...
- 第31题:LeetCode946. Validate Stack Sequences验证栈的序列
题目 给定 pushed 和 popped 两个序列,只有当它们可能是在最初空栈上进行的推入 push 和弹出 pop 操作序列的结果时,返回 true:否则,返回 false . 示例 1: 输入: ...
- (栈)leetcode 946. Validate Stack Sequences
Given two sequences pushed and popped with distinct values, return true if and only if this could ha ...
随机推荐
- 转成p进制算法C语言
今天打比赛的时候竟然下一没有想起来, 实际上是非常简单的. 举例说明: $64 = 2 \times 3^3 + 1 \times 3^2 + 3^0$ 根据秦九韶算法每次提出3,即 $3(2 \ti ...
- JPA EnableJpaAuditing 审计功能
关于自动填充或更新实体中的 CreateDate.CreatedBy 等在之前有一篇 jeecg 默认为空的字段值是如何被填充的? 有提到通过拦截器的方式实现,但是今天带大家了解一下如果使用 JPA ...
- Tips on Java
1.JAVA种数组的两种定义方式. int[] nums; int nums[]. 2.整型默认为int,如果需要long,须加l或L.小数默认double,d或D可省略,但如果需要float,须加f ...
- LeetCode 1140. Stone Game II
原题链接在这里:https://leetcode.com/problems/stone-game-ii/ 题目: Alex and Lee continue their games with pile ...
- Vigil 发送多人邮件通知的处理
Vigil 默认是只能发送单人邮件,但是我们有需要发送多个的场景. 解决方法: 大家使用一样的账户登陆 使用邮件组 修改下源码 为了学习下Vigil 的构建,以及原理,我简单通过修改源码的方式(目前支 ...
- 1-ESP8266 SDK开发基础入门篇--开发环境搭建
因为今天终于做好了自己的另一块工控板,所以我就开始写基础公开篇的内容,希望自己小小的努力能够帮到大家 自己做的另一块板子 https://www.cnblogs.com/yangfengwu/cate ...
- SDSC2019【游记】
目录 SDSC2019 游记 Day0 Day 1 Day2 Day3 Day4 Day5 Day6 Day 7 Day8 SDSC2019 游记 Day0 这次夏令营在日照某大学举行,我很想夸一夸喷 ...
- 你对SQA的职责和工作活动(如软件度量)的理解?
SQA就是独立于软件开发的项目组,通过对软件开发过程的监控,来保证软件的开发流程按照指定的CMM规程(如果有相应的CMM规程),对于不符合项及时提出建议和改进方案,必要时可以向高层经理汇报以求问题的解 ...
- ICEM-圆角正方体
原视频下载地址:https://pan.baidu.com/s/1c2cNgJm 密码: rci8
- rust结构体
//Rust 并不允许只将某个字段标记为可变 struct User { email: String, name:String, age:i32, sex:String, active:bool, } ...