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. c语言实践输出某个区间中不是3的倍数的偶数

    OK,先审题,我们最后要输出的那些数是需要满足两个条件的,第一个条件是,这个数不是3的倍数,第二个条件是这个数是偶数.也就是这样的数需要同时满足这两个条件的时候才把这个数输出. 不是3的倍数这个条件在 ...

  2. U14.04 teamviewer install

    转载http://www.cnblogs.com/kunyuanjushi/p/5205639.html 安装i386库 sudo apt-get install libc6:i386 libgcc1 ...

  3. numpy.loadtxt() 出现codecError_____ Excel 做矩阵乘法

    1) 用 numpy读入csv文件是报错 UnicodeDecodeError: 'gbk' codec can't decode byte 0xbf in position 2: illegal m ...

  4. Map存储容量及内存占用测试

    Integer a = 1; long start = 0; long end = 0; // 先垃圾回收 System.gc(); start = Runtime.getRuntime().free ...

  5. JS和JQuery的比较

    一. Jquery它是javascript的一个轻量级框架,是对javascript进行封装. 二.JQuery和JS都有加载函数,但表达方式不同. 1.JS中的加载函数: //整个文档加载完毕后执行 ...

  6. 【转】android 手势识别和VelocityTracker

    参考地址: http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1020/448.html http://www.jcodecraeer.co ...

  7. xcode工程配置绝对路径与相对路径

    1.问题描述 一般我们在xcode里面配置包含工程目录下头文件的时候,都要关联着相对路径和绝对路径,如果只是自己用这个项目,用绝对路径的问题不大,但是如果你把工程发给别人,别人就要在改这个绝对路径,这 ...

  8. pycharm设置连接

    https://blog.csdn.net/u013088062/article/details/50100121

  9. 如何修改git显示的用户名

    我是这样试了一下,可以改: 输入修改用户名和邮箱: $git config --global user.email "tanteng@gmail.com" $git config ...

  10. 【TJOI2017】异或和

    题目描述 在加里敦中学的小明最近爱上了数学竞赛,很多数学竞赛的题目都是与序列的连续和相关的.所以对于一个序列,求出它们所有的连续和来说,小明觉得十分的简单.但今天小明遇到了一个序列和的难题,这个题目不 ...