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. 如何使用keil5将stm32的hal库编译成lib文件——F1版本

    hal库中keil5中编译的速度是比较慢的,相同情况下,每次都要编译的时候,比标准库是要慢很多的,因此就hal库编译成lib文件是一种加快编译速度的方法,当然也有其自身的缺点.一.步骤1.使用cube ...

  2. 使用vue-cli3搭建项目过程

    一.搭建前准备 node.js版本为8.9+: 安装模块:npm install -g n // 安装模块 这个模块是专门用来管理node.js版本的: 若原先已经安装,则更细模块:n stable ...

  3. UVa11542Squre——异或方程组&&高斯消元法

    题意 给出 $n$ 个整数,从中选出1个或多个,使得选出的整数乘积是完全平方数.一共有多少种选法?($1 \leq n \leq 100$,$1 \leq a_i \leq 10^{15}$ 且不含大 ...

  4. 创建nextcloud所需的数据库和账户

      创建 nextcloud 所需的数据库和账户 打开数据库管理命令行,默认root没密码,回车进入 sudo mysql -u root -p 创建 nextcloud 数据库,命令包含后面的分号 ...

  5. 【转载】Visual Studio Code 构建 C/C++ 开发环境

    https://www.cnblogs.com/XieSir/articles/8288051.html 1. 安装 MinGW Distro / MinGW / GNU GCC 中的任何一款,( W ...

  6. Python面向对象 -- slots, @property、多重继承MixIn、定制类(str, iter, getitem, getattr, call, callable函数,可调用对象)、元类(type, metaclass)

    面向对象设计中最基础的3个概念:数据封装.继承和多态 动态给class增加功能 正常情况下,当定义了一个class,然后创建了一个class的实例后,可以在程序运行的过程中给该实例绑定任何属性和方法, ...

  7. Nodejs中的JavaScript

    一.Ecmascript ①基本语法:if var function Object Array等 ②特别注意:Node.js中没有DOM和BOM 二.核心模块: 1.Node.js为JavaScrip ...

  8. 洛谷 P2004 领地选择 题解

    P2004 领地选择 题目描述 作为在虚拟世界里统帅千军万马的领袖,小Z认为天时.地利.人和三者是缺一不可的,所以,谨慎地选择首都的位置对于小T来说是非常重要的. 首都被认为是一个占地C*C的正方形. ...

  9. mysql下sql语句令某字段值等于原值加上一个字符串

    MYSQL在一个字段值后面加字符串,如下: member 表名 card 字段名 update member SET card = '00' || card; (postgreSQL 用 || 来连贯 ...

  10. Git创建与合并分支,撤销修改

    git回滚到指定版本并推送到远程分支(撤销已提交的修改,并已push) git reset --hard <commit ID号> git push -f git回滚到上一个版本并推送到远 ...