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.

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.

判断栈的合法性

class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
if(pushed.size()==&&pushed.size()==){
return true;
}
if(pushed.size()!=popped.size()){
return false;
}
stack<int>s;
int index = ,outdex = ;
while(index<pushed.size()&&outdex<=popped.size()){
if(pushed[index]==popped[outdex]){
index++;
outdex++;
}else if(!s.empty()&&popped[outdex]==s.top()){
while(!s.empty()&&popped[outdex]==s.top()){
s.pop();
outdex++;
}
}else{
s.push(pushed[index]);
index++;
}
if (index >= popped.size())//如果入栈序列已经走完,出栈序列和栈顶元素一一比较
{
while (!s.empty() && popped[outdex]==s.top())
{
s.pop();
outdex++;
} //如果和栈中比较完,两个序列都走完了,即表明顺序合法
if (index == pushed.size() && outdex == popped.size())
{
return true;
}
else
{
return false;
}
}
}
}
};
 

112th LeetCode Weekly Contest Validate Stack Sequences的更多相关文章

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

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

  2. 【leetcode】946. Validate Stack Sequences

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

  3. 112th LeetCode Weekly Contest Minimum Increment to Make Array Unique

    Given an array of integers A, a move consists of choosing any A[i], and incrementing it by 1. Return ...

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

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

  5. Leetcode 946. Validate Stack Sequences 验证栈序列

    946. Validate Stack Sequences 题目描述 Given two sequences pushed and popped with distinct values, retur ...

  6. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  7. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  8. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  9. 946. Validate Stack Sequences

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

随机推荐

  1. Java 基于web service 暴露接口 供外部调用

    package cn.zr.out.outinterface; import java.text.SimpleDateFormat; import java.util.Date; import jav ...

  2. 19、SOAP安装,运用与比对结果解释

    转载:http://www.dengfeilong.com/post/Soap2.html https://blog.csdn.net/zhu_si_tao/article/details/71108 ...

  3. 20169219 使用Metaspoit攻击MS08-067实验报告

    MS08-067漏洞介绍 MS08-067漏洞的全称为"Windows Server服务RPC请求缓冲区溢出漏洞",如果用户在受影响的系统上收到特制的 RPC 请求,则该漏洞可能允 ...

  4. c++基础知识篇:指针

    从面试的反馈来看,这部分可以问的很难. 1.指针与引用的区别 指针是一个变量,用来存放地址的变量.引用是原来变量的存储空间的别名.   2.指针作为参数的要点 a.需要进行指针的合法性检验,防止空指针 ...

  5. wp socket tcp链接

    using System; using System.Net; /// <summary> /// 客户端通过TCP/IP连接服务端的方法,包含连接,发送数据,接收数据功能 /// < ...

  6. Autofac的Autofac.Core.Activators.Reflection.DefaultConstructorFinder错误解决方案。

    在使用Autofac的时候,不给力,看着例子来的,人家没问题,我就报了Autofac.Core.Activators.Reflection.DefaultConstructorFinder错误. 百般 ...

  7. MongoDB插入时间不正确的问题

    关于mongodb插入时间不正确的问题 今天在给mongodb插入日期格式的数据时发现,日期时间相差8个小时,原来存储在mongodb中的时间是标准时间UTC +0:00,而中国的时区是+8.00 . ...

  8. 20165219 2017-2018-2 《Java程序设计》第4周学习总结

    20165219 2017-2018-2 <Java程序设计>第4周学习总结 课本知识总结 第五章 在java中,继承时使用extends关键字,private成员也会被继承,只不过子类无 ...

  9. SHTSC2017酱油记

    考完回来累成狗..睡了一觉..补游记.. DAY0 把最近刷的题发了下题解..NOIP RK10的蒟蒻收拾收拾准备退役了.. 12点就睡了..很久周五没这么早睡了.. DAY1 9点就醒了..莫名紧张 ...

  10. c++11时间相关库(chrono)

    以下整理自:https://www.2cto.com/kf/201404/290706.html chrono 库主要包含了三种类型:时间间隔 Duration.时钟 Clocks 和时间点 Time ...