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. 计量经济与时间序列_关于Box-Jenkins的ARMA模型的经济学意义(重要思路)

    1 很多人已经了解到AR(1)这种最简单的时间序列模型,ARMA模型包括AR模型和MA模型两个部分,这里要详细介绍Box-Jenkins模型的观念(有些资料中把ARMA模型叫做Box-Jenkins模 ...

  2. html title属性内容换行方法(静态页面)

    鼠标经过悬停于对象时提示内容(title属性内容)换行排版方法,html title 换行方法总结. html的title属性默认是显示一行的,这里介绍两种换行方法为大家分享使用. 一.直接title ...

  3. AUTOSAR-Specification of Watchdog Manager 阅读

    一.开门狗管理有三种机制 1.定周期任务实时监控 2.非定周期任务执行时间监控 3.逻辑监控,执行顺序. 二.受监控的实体和检查点 Watchdog Manager监督软件的执行.监督的逻辑单位是受监 ...

  4. java多线程高并发的学习

    1.      计算机系统 使用高速缓存来作为内存与处理器之间的缓冲,将运算需要用到的数据复制到缓存中,让计算能快速进行:当运算结束后再从缓存同步回内存之中,这样处理器就无需等待缓慢的内存读写了. 缓 ...

  5. rsync实战(二)

    加两个需求:1.增加一个模块2.每个模块不同的用户名 步骤: .修改配置文件/etc/rsyncd.conf [backup] comment = commit path = /backup auth ...

  6. Office 365 邮件流

    进入Exchange管理中心->点击左侧的“邮件流”->进入邮件流配置页面. 一.规则 规则也称传输规则,对通过组织传递的邮件,根据设定条件进行匹配,并对其进行操作.传输规则与众多电子邮件 ...

  7. MySQL安装教程及Navicat连接MySQL报错:1251-Client does not support authentication protocol requested by server

    MySQL安装可参考: MySql 8.0.18安装 此参考文章后面涉及到的密码修改,对本标题碰到的错误同样适用. 本文先讲如何安装,在讲碰到的1251问题.要直接看解决方案的朋友可以直接通过目录链接 ...

  8. SPACESNIFFER查看文件大小

  9. [LC] 59. Spiral Matrix II

    Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral ord ...

  10. 吴裕雄--天生自然python学习笔记:python用 Bokeh 模块绘制我国 GDP 数据统计图

    现在我们把我国 1990 年到 2017 年的 GDP 数据抓取出 来,井用 Bokeh 绘 出散点统计图 . 由网页爬取所需数据,并用 Bokeh 绘制散点图 . import requests # ...