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. Java 开发者必须了解的 16 个Java 顶级开源项目!

    本文已经收录自笔者开源的 JavaGuide: https://github.com/Snailclimb/JavaGuide ([Java学习+面试指南] 一份涵盖大部分Java程序员所需要掌握的核 ...

  2. gitlab安装教程

    gitlab安装教程     安装教程 官网安装方法 https://about.gitlab.com/downloads/#centos7 1.准备 sudo yum install curl po ...

  3. debian8.8更新源

    ##163源 deb http://mirrors.163.com/debian/ jessie main non-free contribdeb http://mirrors.163.com/deb ...

  4. codeforce 1188A1 Add on a Tree 树

    题意:给你一个树,有一种操作,选择两个叶子节点,然后把这两个叶子节点间的路径全部加或减一个值.问你给出的树上的每一条边经过若干次操作是否可以为任意值. 分析:画几个图后可以发现,如果树中存在一个点的度 ...

  5. day55-mysql-用户权限、修改秘密、忘记密码

    .用户权限:新创建的用户没有库,如果想让新用户访问我的库,必须给它授权才可以.我在使用的navicat要关闭新用户的连接才可以授权给它. .创建用户 '; -- 创建用户 .移除用户 drop use ...

  6. 视觉SLAM算法框架解析(2) ORB-SLAM

    版权声明:本文为博主原创文章,未经博主允许不得转载. ORB-SLAM[1]完全继承了PTAM(http://www.cnblogs.com/zonghaochen/p/8442699.html)的衣 ...

  7. python_检测一些特定的服务端口有没有被占用

    一个python端口占用监测的程序,该程序可以监测指定IP的端口是否被占用. #!/usr/bin/env python# -*- coding:utf-8 -*- import socket, ti ...

  8. K - Wand(组合数+错排公式)

    N wizards are attending a meeting. Everyone has his own magic wand. N magic wands was put in a line, ...

  9. ubantu中的mysql命令

    查看mysql的安装目录:which mysql 进入mysql的运行状态:mysql -uroot -p 56..a_

  10. Nginx 反向代理,IP、端口,项目路径变化的问题

    这两天在云上部署公司项目,涉及到nginx反向代理,在部署完成测试,发现在下载文件的时候,无法下载,提示链接被拒绝. 假设nginx代理地址: http://121.53.21.188:9012/we ...