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:

Input: pushed = [1,2,3,4,5], popped = [4,5,3,2,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:

Input: pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
Output: false
Explanation: 1 cannot be popped before 2.

Note:

  1. 0 <= pushed.length == popped.length <= 1000
  2. 0 <= pushed[i], popped[i] < 1000
  3. pushed is a permutation of popped.
  4. 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();
}
}

参考资料

LeetCode 946. 验证栈序列(Validate Stack Sequences) 26的更多相关文章

  1. [Swift]LeetCode946. 验证栈序列 | Validate Stack Sequences

    Given two sequences pushed and popped with distinct values, return true if and only if this could ha ...

  2. Leetcode 946. Validate Stack Sequences 验证栈序列

    946. Validate Stack Sequences 题目描述 Given two sequences pushed and popped with distinct values, retur ...

  3. 946. Validate Stack Sequences

    946. Validate Stack Sequences class Solution { public: bool validateStackSequences(vector<int> ...

  4. 【LeetCode】946. Validate Stack Sequences 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟过程 日期 题目地址:https://leetc ...

  5. 【leetcode】946. Validate Stack Sequences

    题目如下: Given two sequences pushed and popped with distinct values, return true if and only if this co ...

  6. 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 ...

  7. 946. Validate Stack Sequences验证栈序列

    网址:https://leetcode.com/problems/validate-stack-sequences/ 参考:https://leetcode.com/problems/validate ...

  8. 第31题:LeetCode946. Validate Stack Sequences验证栈的序列

    题目 给定 pushed 和 popped 两个序列,只有当它们可能是在最初空栈上进行的推入 push 和弹出 pop 操作序列的结果时,返回 true:否则,返回 false . 示例 1: 输入: ...

  9. (栈)leetcode 946. Validate Stack Sequences

    Given two sequences pushed and popped with distinct values, return true if and only if this could ha ...

随机推荐

  1. B君的历史——复数乘法&&爆搜

    题意 设 $r = \frac{-1+\sqrt7 i}{2}$,对任意整数 $x, y$ 都可以找到一个有限的整数集合 $S$,使得 $$x + y\sqrt7 i = \sum_{k \in S ...

  2. MySql数据封装操作类

    1.先引用MySQL的DLL文件 using System; using System.Collections.Generic; using System.Linq; using System.Tex ...

  3. lambda表达式格式以及应用场景?

    lambda表达式,通常是在需要一个函数,但是又不想费神去命名一个函数的场合下使用,也就是指匿名函数. add = lambda x, y : x+y print(add(1,2)) # 结果为3 应 ...

  4. Jmeter扩展自定义函数

    步骤1.导入lib\ext下ApacheJMeter_core.jar和ApacheJMeter_functions.jar 步骤2.新建function的类的package声明必须已".f ...

  5. Goexit

    package main import ( "fmt" "runtime" ) func test() { defer fmt.Println("cc ...

  6. CSP2019 J组 游记

    结果 分数出来了.100+100+10+35=245. 一等线230,擦着边进一等. (点击图片放大) 期待明年s组的表现. 第一轮 不就是初赛吗?擦边轻松水过去! 第二轮 Day -14 停两周晚自 ...

  7. 【洛谷P4319】 变化的道路 线段树分治+LCT

    最近学了一下线段树分治,感觉还蛮好用... 如果正常动态维护最大生成树的话用 LCT 就行,但是这里还有时间这一维的限制. 所以,我们就把每条边放到以时间为轴的线段树的节点上,然后写一个可撤销 LCT ...

  8. 使用singer tap-postgres 同步数据到pg

    singer 是一个很不错的开源etl 解决方案,以下演示一个简单的数据从pg 同步到pg 很简单就是使用tap-postgres + target-postgres 环境准备 对于测试的环境的数据库 ...

  9. 洛谷 P2813【母舰】 题解

    总体思路: 输入护盾和攻击力,然后快速排序sort走起来, 排完序之后从第一个开始找,如果攻击力大于护盾,护盾继续下一个, 这个攻击力记录为0,如果小雨的话,那就攻击力继续下一个,护盾不动, 其中最为 ...

  10. P2624 [HNOI2008]明明的烦恼

    #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #inclu ...