题目

给定 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. 再谈布局,栅栏式自适应布局的学习和实现(calc自适应布局)

    布局真的很重要.一个不好的布局后期会有很多很多的bug,就像是建房子的地基一样. 首先,再一次地圣杯布局的学习,来源于该教程: http://www.jianshu.com/p/f9bcddb0e8b ...

  2. Python网络爬虫(二)

    Urllib库之解析链接 Urllib库里有一个parse这个模块,定义了处理URL的标准接口,实现 URL 各部分的抽取,合并以及链接转换.它支持如下协议的 URL 处理:file.ftp.goph ...

  3. springBoot实现socketio

    https://github.com/mrniko/netty-socketio-demo https://github.com/mrniko/netty-socketio

  4. 012 Integer to Roman 整数转换成罗马数字

    给定一个整数,将其转为罗马数字.输入保证在 1 到 3999 之间. 详见:https://leetcode.com/problems/integer-to-roman/description/ cl ...

  5. 模拟虚拟的文件系统initrd/initramfs

    请看initramfs文件的以下解析: [root@ant-colonies boot]# ls config--.el6.x86_64 lost+found efi symvers--.el6.x8 ...

  6. SpringBoot | 第二章:lombok介绍及简单使用

    在去北京培训的时候,讲师说到了lombok这个第三方插件包,使用了之后发现,确实是个神奇,避免了编写很多臃肿的且定式的代码,虽然现代的IDE都能通过快捷键或者右键的方式,使用Generate Gett ...

  7. 2 cmd中startup显示运行不了显示“不是内部或外部命令”

    解决方案: 1 在C:\Windows\System32中检查cmd.exe是否存在(如果存在的话)(检查cmd.exe是否被误删) 2 在我的电脑——属性——环境变量——在系统变量找到Path编辑前 ...

  8. css3动画-加载中...

    写几个简单的加载中动画吧. 像前面三种都是相当于几个不同的点轮流来播放同一动画:变大变小.css3里面有一个用于尺度变换的方法:scale(x,y):定义 2D 缩放转换,改变元素的宽度和高度. 第四 ...

  9. form表单上传域(type="file")的使用----上传文件

    一,单个文件的上传 1.html/jsp页面 <%@ page language="java" contentType="text/html; charset=UT ...

  10. 构建第一个Spring Boot2.0应用之集成dubbo上---环境搭建(九)

    一.环境: Windows: IDE:IntelliJ IDEA 2017.1.1 JDK:1.8.0_161 Maven:3.3.9 springboot:2.0.2.RELEASE Linux(C ...