第31题:LeetCode946. Validate Stack Sequences验证栈的序列
题目
给定
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 之前弹出。
提示:
0 <= pushed.length == popped.length <= 10000 <= pushed[i], popped[i] < 1000pushed是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验证栈的序列的更多相关文章
- Leetcode946. Validate Stack Sequences验证栈序列
给定 pushed 和 popped 两个序列,只有当它们可能是在最初空栈上进行的推入 push 和弹出 pop 操作序列的结果时,返回 true:否则,返回 false . 示例 1: 输入:pus ...
- Leetcode 946. Validate Stack Sequences 验证栈序列
946. Validate Stack Sequences 题目描述 Given two sequences pushed and popped with distinct values, retur ...
- 946. Validate Stack Sequences验证栈序列
网址:https://leetcode.com/problems/validate-stack-sequences/ 参考:https://leetcode.com/problems/validate ...
- LeetCode 946. 验证栈序列(Validate Stack Sequences) 26
946. 验证栈序列 946. Validate Stack Sequences 题目描述 Given two sequences pushed and popped with distinct va ...
- 946. Validate Stack Sequences
946. Validate Stack Sequences class Solution { public: bool validateStackSequences(vector<int> ...
- [Swift]LeetCode946. 验证栈序列 | Validate Stack Sequences
Given two sequences pushed and popped with distinct values, return true if and only if this could ha ...
- (栈)leetcode 946. Validate Stack Sequences
Given two sequences pushed and popped with distinct values, return true if and only if this could ha ...
- 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 ...
- 【leetcode】946. Validate Stack Sequences
题目如下: Given two sequences pushed and popped with distinct values, return true if and only if this co ...
随机推荐
- input[checkbox],input[radiobox]的一些问题
复选框和文字对不齐:checkbox复选框的一些深入研究与理解: 解决方案:复选框或单选框与文字对齐的问题的深入研究与一 实例:实例.
- 关于MySQL AUDIT(审计)那点事
2017年06月02日MySQL社区版本最新版为MySQL_5.7.18,但是该版本不带AUDIT功能(MySQL Enterprise Edition自带AUDIT功能),因此需要加载plugin( ...
- Python 简单的方法爬取b站dnf视频封面
import urllib.request cnt=0 def instr(keystr): st=keystr.find('(')+1 strhtml=keystr[st:len(keystr)-1 ...
- GUI的最终选择 Tkinter(二):Label和Button组件
Label组件 Lable组件是用于界面上输出描述的标签,例如提示用户“您下载的电影含有未成年人限制内容,请满18岁以后点击观看!”,先来上结果图: 在来看下它的代码: from tkinter im ...
- win10卸载更新+关闭自动更新的方法
卸载更新方法: 左下角->设置->更新和安全->windows更新->高级选项->查看更新历史记录->卸载更新 关闭自动更新的方法:win+r 输入service ...
- deep copy and shallow copy
链接A:浅拷贝就是成员数据之间的一一赋值:把值赋给一一赋给要拷贝的值.但是可能会有这样的情况:对象还包含资源,这里的资源可以值堆资源,或者一个文件..当值拷贝的时候,两个对象就有用共同的资源,同时对资 ...
- Linux网络管理命令ifdown/ifup与ifconfig/ip中的down/up命令的对比
参考了:https://blog.csdn.net/GDUTLYP/article/details/50498202 以下网卡均采用eth1说明. 相同点——[启用]和[禁止]网卡 ifdown et ...
- superset 配置连接 hbase
1. 简单说明 最近配置superset查询hbase, 根据网上查询到的文档和经验,成功了一次(python3.4 superset 0.20.),后边重试换各种版本就不行了.最后根据错误终于发现 ...
- linux系统redis配置环境变量
1.测试:在任何位置登录redis redis-cli 指定服务器ip(不指定时,默认本机) redis-cli -h 127.0.0.1 指定端口(不指定时,默认6379) redis-cli -h ...
- springboot 学习笔记(八)
springboot整合activemq,实现queue,topic同时支持 1.JMS中定义了两种消息模型:点对点(point to point, queue)和发布/订阅(publish/subs ...