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 ...
随机推荐
- vue饿了么UI库-笔记
1. :rules="{required: true, message: '有效期不能为空'}" :rules="{type:'date',required: true, ...
- Generator 函数和for...of循环,实现斐波那契数列
function* fib () { let [prev, cur] = [0,1] for (;;) { yield cur [prev, cur] = [cur, cur+prev] } } fo ...
- 代码中的mysql语法问题
今天在代码中写了mysql的删除语句 String lpinsuredSQL=" delete from lpinsured a where a.insuredid='?InsuredID? ...
- LeetCode 723. Candy Crush
原题链接在这里:https://leetcode.com/problems/candy-crush/ 题目: This question is about implementing a basic e ...
- shell脚本 mysql主从
#!/bin/bash systemctl stop firewalld setenforce user="tom" password=" slave_ipaddr=&q ...
- Redis存储Sortedset
与set相比Sortedset多了一个数字与set中的各个元素相关联. 存储结构: 1.添加元素: 添加元素的时候元素一定不能相同,如果已存在该元素,表示插入失败返回0,成功返回1,但是不同元素的数字 ...
- 使用rrweb 进行web 操作录制以及回放
rrweb 是使用typescript 开发的web 操作录制以及回放框架,包含了比较完整的系统组件 rrweb-snapshot 进行dom 与操作实践的关联处理 rrweb 主要包含了record ...
- 移动端tap事件(轻击、轻触)
一.问题 ①移动端也有click点击事件,click点击会延迟200~300ms ②因为点击的响应过慢,影响了用户体验,所以需要解决响应慢的问题 二.解决方案 ①使用tap事件:即轻击,轻敲,响应速度 ...
- [USACO09DEC] Dizzy Cows 拓扑序
[USACO09DEC] Dizzy Cows 拓扑序 先对有向边跑拓扑排序,记录下每个点拓扑序,为了使最后的图不存在环,加入的\(p2\)条无向边\(u,v\)必须满足\(u\)拓扑序小于\(v\) ...
- 56、Spark Streaming: transform以及实时黑名单过滤案例实战
一.transform以及实时黑名单过滤案例实战 1.概述 transform操作,应用在DStream上时,可以用于执行任意的RDD到RDD的转换操作.它可以用于实现,DStream API中所没有 ...