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. OpenCV On Android环境配置最新&最全指南(Eclipse篇)

    简介 本教程是经过本人多次踩坑,并参考网上众多OpenCV On Android的配置教程总结而来,尽希望能帮助学习移动图像处理的朋友们少走弯路.这也是本人第一次在简书上发布文章,如有不足,希望各位d ...

  2. Apsara Clouder云计算技能认证:云数据库管理与数据迁移

    一.课程介绍 二.云数据库的简介及使用场景 1.云数据库简介 1.1特点: 用户按存储容量和带宽的需求付费 可移植性 按需扩展 高可用性(HA) 1.2阿里云云数据库 RDS 稳定可靠,可弹性伸缩的在 ...

  3. 添加新硬盘,扩展Centos7根分区

    ##背景介绍,系统安装时,分配的硬盘容量太小,根分区空间不够用,现添加一个新硬盘,通过以下步骤来扩展centos7根分区 [root@t201 ~]# df -h 文件系统 容量 已用 可用 已用% ...

  4. kaggle——绝地求生游戏最终排名预测

    绝地求生游戏最终排名预测 知识点 数据读取与预览 数据可视化 构建随机森林预测模型 导入数据并预览 先导入数据并预览.本次实验同样来源于 Kaggle 上的一个竞赛: 绝地求生排名预测 ,由于原始数据 ...

  5. Super Mario HDU - 4417 (主席树询问区间比k小的个数)

    Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory ...

  6. 传统的Servlet在spring boot中怎么实现的?

    传统的Servlet在spring boot中怎么实现的? 本文主要内容: 1:springboot一些介绍 2:传统的servlete项目在spring boot项目中怎么实现的?web.xml.u ...

  7. Office VBA开发经典-中级进阶卷 配套资源下载

    本书源代码请到如下页面寻找: https://www.cnblogs.com/ryueifu-VBA/p/8982192.html

  8. Linux系统安装及配置ftp服务

    1. 先用rpm -qa| grep vsftpd命令检查是否已经安装,如果ftp没有安装,使用yum  -y  install vsftpd 安装,(ubuntu 下使用apt-get instal ...

  9. php获取服务器和mysql等信息输出到页面(基于ci框架)

    function show($varName) {    switch($result = get_cfg_var($varName)) {     case 0:      return '< ...

  10. F5 BIG-IP LTM会话保持