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.

示例

示例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

示例2

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

解答

这道题很简单,把pushed数组里面的每个数字入栈,然后当push的值和popped里面的第一个未处理的值相等的时候,就进入循环,将popped数组里面的值从栈中移除。

最后判断popped数组里面的值是否处理完。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
大专栏  Leetcode 946. Validate Stack Sequences 验证栈序列ine">17
18
19
20
21
22
class  {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
stack<int> mystack;
int i = 0; for (auto &s : pushed) {
mystack.push(s); while (!mystack.empty() && mystack.top() == popped[i]) {
i ++; mystack.pop();
} }
if (mystack.empty() && i == popped.size()){
return true;
}
return false;
}
};

Leetcode 946. Validate Stack Sequences 验证栈序列的更多相关文章

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

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

  2. Leetcode946. Validate Stack Sequences验证栈序列

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

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

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

  4. (栈)leetcode 946. Validate Stack Sequences

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

  5. 946. Validate Stack Sequences

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

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

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

  7. 【leetcode】946. Validate Stack Sequences

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

  8. LeetCode 946. 验证栈序列(Validate Stack Sequences) 26

    946. 验证栈序列 946. Validate Stack Sequences 题目描述 Given two sequences pushed and popped with distinct va ...

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

随机推荐

  1. 箭头函数,闭包函数中的this指向

    在javscript中,this 是在函数运行时自动生成的一个内部指针,它指向函数的调用者. 箭头函数有些不同,它的this是继承而来, 默认指向在定义它时所处的对象(宿主对象),而不是执行时的对象. ...

  2. 电影评论分类:二分类问题(IMDB数据集)

    IMDB数据集是Keras内部集成的,初次导入需要下载一下,之后就可以直接用了. IMDB数据集包含来自互联网的50000条严重两极分化的评论,该数据被分为用于训练的25000条评论和用于测试的250 ...

  3. linux服务器CentOS7安装node.js

    方维一元夺宝2.0版本,很多用户面临机器人自动执行任务.采集计划一直无法开启的问题. 这个需要开启node.js分享给大家. 1.获取node.js资源 V5.x: curl --silent --l ...

  4. Java中:>>>和>>区别

    >>>表示不带符号向右移动二进制数,移动后前面统统补0:两个箭头表示带符号移动, 没有<<<这种运算符,因为左移都是补零,没有正负数的区别. 如 -12 的二进制为 ...

  5. git的基础使用

    GIT """ 什么是git:版本控制器 - 控制的对象是开发的项目代码 代码开发时间轴:需求1 > 版本库1 > 需求2 > 版本库2 > 版本 ...

  6. CodeForces 996B World Cup(思维)

    https://codeforces.com/problemset/problem/996/B 题意: 圆形球场有n个门,Allen想要进去看比赛.Allen采取以下方案进入球场:开始Allen站在第 ...

  7. LibraryBuilder——从元器件datasheet到Library

    LibraryBuilder是Cadence推出的元件库管理工具,可以从PDF自动创建器件的原理图符号及PCB封装. 软件可以从“吴川斌的博客”下载到. 这里大致记录一下创建元件库的过程,以Beagl ...

  8. 14 微服务电商【黑马乐优商城】:day04-项目搭建(二)

    本项目的笔记和资料的Download,请点击这一句话自行获取. day01-springboot(理论篇) :day01-springboot(实践篇) day02-springcloud(理论篇一) ...

  9. Git与IDEA集成

    软件配置: 系统版本:Windows10 JDK版本:1.8 Git版本:2.19.1 IDEA版本:2016.3 Maven版本:3.5.4 Git安装: Git下载地址:https://git-s ...

  10. mock简单的json返回

    针对非常简单的json返回串,我们也不一定非得通过freemarker模板的方式来构造返回数据,这里看实际的需求,如果返回的内容是固定的,而且json又非常简单,我们也可以直接写在程序里面,下面的接口 ...