题目

给定 pushed 和 popped 两个序列,只有当它们可能是在最初空栈上进行的推入 push 和弹出 pop 操作序列的结果时,返回 true;否则,返回 false 。

示例 1:

输入:pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
输出:true
解释:我们可以按以下顺序执行:
push(1), push(2), push(3), push(4), pop() -> 4,
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1

示例 2:

输入:pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
输出:false
解释:1 不能在 2 之前弹出。

提示:

  1. 0 <= pushed.length == popped.length <= 1000
  2. 0 <= pushed[i], popped[i] < 1000
  3. pushed 是 popped 的排列。

考点

1.stack

2.vector


思路

输入:pushed = [1,2,3,4,5], popped = [4,5,3,2,1]

输出:true

输入:pushed = [1,2,3,4,5], popped = [4,3,5,1,2]

输出:false

函数入口做鲁棒性测试,都为空 ->true; size不同->false

flag=false;

定义两个iterator访问pushed和popped容器,用于读取元素,并且判断匹配成功标准

auto pushIt=pushed.begin();

大循环(popped没有访问完时)

{

入栈操作: data为空或者data.top!=*popIt时:

{

1.1 有数可以入:pushIt!=pushed.end() :

{

data.push(*pushIt);

pushIt++;

}

1.2 否则无数可入: break;

}

否则出栈:

{

data.pop();

popIt++:

}

}

匹配成功条件:stack为空,且popIt访问到了popped.end()


代码

newcoder

class Solution {
public:
bool IsPopOrder(vector<int> pushV,vector<int> popV) {
bool flag = false; if(!pushV.size()||!popV.size())
return flag; //定义栈,读取迭代器
stack<int> data;
auto pushNext=pushV.begin();
auto popNext=popV.begin(); while(popNext!=popV.end())
{ //入栈
if(data.empty()||data.top()!=*popNext)
{
//pushed没有可入栈元素,返回
if(pushNext==pushV.end())
break;
else//否则,压入栈中
{
data.push(*pushNext);
pushNext++;
}
}
else//pop
{
data.pop();
popNext++;//访问下个poped元素
} } //如果压入栈为空,且poped序列访问完,则返回真
if(data.empty()&&popNext==popV.end())
flag=true; return flag;
}
};

leetcode

class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
if(pushed.empty()&&popped.empty())
return true; if(pushed.size()!=popped.size())
return false; bool flag=false; //局部变量
auto pushIt=pushed.begin();
auto popIt=popped.begin();
stack<int> data; while(popIt!=popped.end())
{
//进栈
if(data.empty()||data.top()!=*popIt)
{
//无数可进
if(pushIt==pushed.end())
break;
else
{
//进栈,pushIt++
data.push(*pushIt);
pushIt++;
}
}
else
//出栈
{
data.pop();
popIt++;
}
} return (popIt==popped.end()&& data.empty()) ? true : flag ; }
};

问题

1. vector::begin/end

#include <iostream>
#include <vector> int main ()
{
std::vector<int> myvector;
for (int i=1; i<=5; i++) myvector.push_back(i); std::cout << "myvector contains:";
for (std::vector<int>::iterator it = myvector.begin() ; it != myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; return 0;
}
myvector contains: 1 2 3 4 5

用迭代器就不用知道序列的长度了~

容器的迭代器和数组的指针是一个作用。

第31题:LeetCode946. Validate Stack Sequences验证栈的序列的更多相关文章

  1. Leetcode946. Validate Stack Sequences验证栈序列

    给定 pushed 和 popped 两个序列,只有当它们可能是在最初空栈上进行的推入 push 和弹出 pop 操作序列的结果时,返回 true:否则,返回 false . 示例 1: 输入:pus ...

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

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

  3. 946. Validate Stack Sequences验证栈序列

    网址:https://leetcode.com/problems/validate-stack-sequences/ 参考:https://leetcode.com/problems/validate ...

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

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

  5. 946. Validate Stack Sequences

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

  6. [Swift]LeetCode946. 验证栈序列 | Validate Stack Sequences

    Given two sequences pushed and popped with distinct values, return true if and only if this could ha ...

  7. (栈)leetcode 946. Validate Stack Sequences

    Given two sequences pushed and popped with distinct values, return true if and only if this could ha ...

  8. 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 ...

  9. 【leetcode】946. Validate Stack Sequences

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

随机推荐

  1. input 上传文件的判断

    <html> <head> <meta charset='utf-8'> <meta name="viewport" content=&q ...

  2. Leetcode初级算法(字符串篇)

    目录 反转字符串 颠倒整数 字符串中的第一个唯一字符 有效的字母异位词 验证回文字符串 实现strStr() 数数并说 最长公共前缀 字符串转整数(atoi) 反转字符串 和vector同样的进行sw ...

  3. c#spinLock使用

        版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u011915028/article/details/53011811 一下解释摘自msdn  ...

  4. EditPlus常用操作

    EditPlus注册码在线生成 http://www.jb51.net/tools/editplus/ 随意填写个用户名,生成对应的密码就可以使用editplus了 EditPlus常用快捷键 编代码 ...

  5. Oracle存储函数jdbc调用

    package com.jckb.procedure; import java.sql.CallableStatement; import java.sql.Connection; import ja ...

  6. IE6下float双边距问题

    当浮动元素的方向和设置margin的方向相同时,就会出现双边距问题,解决方法是: 删除浮动,改成display:inline-block _display:inline; _zoom:1;

  7. 50个必备的jQuery代码段

    本文会给你们展示50个jquery代码片段,这些代码能够给你的javascript项目提供帮助.其中的一些代码段是从jQuery1.4.2才开始支持的做法,另一些则是真正有用的函数或方法,他们能够帮助 ...

  8. Android入门:封装一个HTTP请求的辅助类

    前面的文章中,我们曾经实现了一个HTTP的GET 和 POST 请求: 此处我封装了一个HTTP的get和post的辅助类,能够更好的使用: 类名:HttpRequestUtil 提供了如下功能: ( ...

  9. 日常bug整理--xxtz

    2017-12-12 建SQLite数据库表时,遇到外键关联报错:foreign key mismatch 解决:发现是个粗心问题,关联的外键没有作为主键,原因是关联的外键由INT改为varchar字 ...

  10. spring的struts简单介绍

    之前一段时间学习了springmvc+mybatis+spring框架,突然对之前的struts东西有点陌生, 所以这里简单记录下温故而知新的东西吧. 1.  首先建立一个Dynamic Web Pr ...