第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 ...
随机推荐
- 关于node中的global,箭头函数的this的一个小问题
this一直是一个JS中的困扰问题,这次在跑JS精粹的代码的时候顺带发现了Node里面全局变量的问题 var x = 1; var myObj = { x: 2 }; myObj.func = fun ...
- python之Selenium库的使用
一 什么是Selenium selenium 是一套完整的web应用程序测试系统,包含了测试的录制(selenium IDE),编写及运行(Selenium Remote Control)和测试的并 ...
- WPF 使用 fontawesome
<Style TargetType="TextBlock" x:Key="tree-icon"> <Style.Setters> < ...
- LDAP--对某些AD属性值是字节数组byte[]情况的类型转换方法
//BitConverter.ToBoolean((searchResult.Properties["mDBUseDefaults"][0] as byte[]), 0); row ...
- Python+Selenium----使用HTMLTestRunner.py生成自动化测试报告2(使用PyCharm )
1.说明 在我前一篇文件(Python+Selenium----使用HTMLTestRunner.py生成自动化测试报告1(使用IDLE ))中简单的写明了,如何生产测试报告,但是使用IDLE很麻烦, ...
- Microsoft JDBC Driver 使用 getParameterMetaData 会报错?
不知道为何使用 Microsoft JDBC Driver for SQL Server 驱动时,sql语句不带参数没有问题,但是如果带参数且使用 getParameterMetaData 就会提示某 ...
- 文件系统满的话(filesystem full),该如何处理。(du and ls)
#!/bin/bash function ergodic(){ ` do "/"$file ] then ergodic $"/"$file else loca ...
- GTY's gay friends 线段树判断区间是否有相同数字
http://acm.hdu.edu.cn/showproblem.php?pid=5172 判断一个区间是否为全排列是: 1.区间总和 = (1 + R - L + 1) * (R - L + 1) ...
- Gym 101047K Training with Phuket's larvae
http://codeforces.com/gym/101047/problem/K 题目:给定n<=2000条绳子,要你找出其中三条,围成三角形,并且要使得围成的三角形面积最小 思路: 考虑一 ...
- Python3.5 调用Ansible 执行命令
ansible.py #!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import tempfile from collections ...