第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 <= 1000
0 <= pushed[i], popped[i] < 1000
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验证栈的序列的更多相关文章
- 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 ...
随机推荐
- 8、kvm虚拟机添加硬盘
kvm虚拟机添加硬盘qemu-img创建一块新的硬盘 qemu-img create -f qcow2 /kvm-data/kvm/jumperhost_disk1.qcow2 50G 关闭虚拟机 v ...
- Django与Ajax,文件上传,ajax发送json数据,基于Ajax的文件上传,SweetAlert插件
一.Django与Ajax AJAX准备知识:JSON 什么是 JSON ? JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻 ...
- 获取Spring应用环境上下文bean
import org.springframework.beans.BeansException; import org.springframework.beans.factory.NoSuchBean ...
- 练习四十六:列表排序,删除list中重复的元素
方法一:使用集合set;将list直接转换为set a = [1,3,4,3,5,7] a = list(set(a)) print(a) 执行结果: [1, 3, 4, 5, 7] 方法二:直接排序 ...
- python_魔法方法(二):算术运算
python2.2之后,对类和类型做了同意,将int().float().str().list().touple()这些BIF转换为工厂函数 >>> type(len) <cl ...
- LeetCode 024 Swap Nodes in Pairs 交换链表中相邻的两个节点
Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2-> ...
- Sqoop环境安装
环境下载 首先将下载的 sqoop-1.4.6.bin__hadoop-2.0.4-alpha.tar.gz放到 /usr/hadoop/目录下(该目录可以自定义,一般为Hadoop集群安装目录),然 ...
- 利用PyQt GUI显示图片、实时播放视频
---作者吴疆,未经允许,严禁转载,违权必究--- ---欢迎指正,需要源码和文件可站内私信联系--- -----------点击此处链接至博客园原文----------- 功能说明:PyQt界面程序 ...
- 详细介绍VO(值对象)和PO(持久对象)的区别
VO,值对象(Value Object),PO,持久对象(Persisent Object),它们是由一组属性和属性的get和set方法组成.从结构上看,它们并没有什么不同的地方.但从其意义和本质上来 ...
- Intellij IDEA 最头大的问题,如何自定义注释模板?
想栈长我当初从 Eclipse 转用 IDEA 真是纠结,放弃然后尝试了N次,不过现在已经算是转型成功了,可以完全脱离 Eclipse 撸码了,虽然说我现在真的撸得非常少了.. 说到 IDEA 的痛点 ...