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. SpringBoot25 gradle安装、利用gradle创建SrpingBoot项目

    1 gradle安装 技巧01:gradle依赖JDK或者JRE,而且版本至少时1.7 1.1 下载安装包 到gradle官网下载安装包[PS: 也可以利用命令的方式安装,本案例是利用安装包的方式] ...

  2. Python pandas DataFrame操作

    1. 从字典创建Dataframe >>> import pandas as pd >>> dict1 = {'col1':[1,2,5,7],'col2':['a ...

  3. python文件复制移动shutil模块

    shutil.copyfile( src, dst) 从源src复制到dst中去.当然前提是目标地址是具备可写权限.抛出的异常信息为IOException. 如果当前的dst已存在的话就会被覆盖掉 s ...

  4. 专题1-MMU-lesson3-MMU配置与使用

    1.段方式MMU 利用虚拟地址然后找到物理地址,通过物理地址访问到led,其过程如下: 一个段的大小是[19:0]总共有1M的地址空间. 从上面可知对应GPIO的段物理基地址是0x7f000000.那 ...

  5. 半平面交 (poj 1279(第一道半平面NlogN)完整注释 )

    半平面交的O(nlogn)算法(转载) 求n个半平面的交有三种做法: 第一种就是用每个平面去切割已有的凸多边形,复杂度O(n^2). 第二种就是传说中的分治算法.将n个半平面分成两个部分,分别求完交之 ...

  6. 理解 RESTful WebService

    RESTful 服务遵循REST(Representational State Transfer)的架构风格,中文翻译为:表现层状态转化 对于所有的CRUD(Read/Create/Update/De ...

  7. WordCountPro

    github项目地址:https://github.com/Hoyifei/SQ-T-Homework-WordCount-Advanced PSP表格   PSP2.1 PSP阶段 预估耗时 (分钟 ...

  8. 编写高质量代码改善C#程序的157个建议——建议24:迭代器应该是只读的

    建议24:迭代器应该是只读的 如果注意观察会发现,FCL中的迭代器只有GetEnumerator方法,没有SetEnumerator方法,所有的集合类也没有一个可以写的迭代器属性.原因有二: 一:这违 ...

  9. ABP源码uml类图

    陆陆续续学习ABP框架有一段时间了,阳光铭睿的入门教程和HK Zhang的源码分析文章对我的学习帮助都很大.之所以会花这么大工夫去学习ABP.看ABP的源代码,一是因为本人对于DDD也非常有兴趣,AB ...

  10. EBS登陆界面个性化

    把完整资料贴出来 Set the profile option Local Login Mask (FND_SSO_LOCAL_LOGIN_MASK). (This profile option is ...