112th LeetCode Weekly Contest Validate Stack Sequences
Given two sequences pushed
and popped
with distinct values, return true
if and only if this could have been the result of a sequence of push and pop operations on an initially empty stack.
Example 1:
Input: pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
Output: true
Explanation: We might do the following sequence:
push(1), push(2), push(3), push(4), pop() -> 4,
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
Example 2:
Input: pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
Output: false
Explanation: 1 cannot be popped before 2.
Note:
0 <= pushed.length == popped.length <= 1000
0 <= pushed[i], popped[i] < 1000
pushed
is a permutation ofpopped
.pushed
andpopped
have distinct values.
判断栈的合法性
class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
if(pushed.size()==&&pushed.size()==){
return true;
}
if(pushed.size()!=popped.size()){
return false;
}
stack<int>s;
int index = ,outdex = ;
while(index<pushed.size()&&outdex<=popped.size()){
if(pushed[index]==popped[outdex]){
index++;
outdex++;
}else if(!s.empty()&&popped[outdex]==s.top()){
while(!s.empty()&&popped[outdex]==s.top()){
s.pop();
outdex++;
}
}else{
s.push(pushed[index]);
index++;
}
if (index >= popped.size())//如果入栈序列已经走完,出栈序列和栈顶元素一一比较
{
while (!s.empty() && popped[outdex]==s.top())
{
s.pop();
outdex++;
} //如果和栈中比较完,两个序列都走完了,即表明顺序合法
if (index == pushed.size() && outdex == popped.size())
{
return true;
}
else
{
return false;
}
}
}
}
};
112th LeetCode Weekly Contest Validate Stack Sequences的更多相关文章
- 【LeetCode】946. Validate Stack Sequences 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟过程 日期 题目地址:https://leetc ...
- 【leetcode】946. Validate Stack Sequences
题目如下: Given two sequences pushed and popped with distinct values, return true if and only if this co ...
- 112th LeetCode Weekly Contest Minimum Increment to Make Array Unique
Given an array of integers A, a move consists of choosing any A[i], and incrementing it by 1. Return ...
- LeetCode 946. 验证栈序列(Validate Stack Sequences) 26
946. 验证栈序列 946. Validate Stack Sequences 题目描述 Given two sequences pushed and popped with distinct va ...
- Leetcode 946. Validate Stack Sequences 验证栈序列
946. Validate Stack Sequences 题目描述 Given two sequences pushed and popped with distinct values, retur ...
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- 946. Validate Stack Sequences
946. Validate Stack Sequences class Solution { public: bool validateStackSequences(vector<int> ...
随机推荐
- Python 安装selenium
一.报错信息 No module named 'selenium' 二.系统环境 操作系统:Win10 64位 Python版本:Python 3.7.0 三.安装参考 1.使用pip安装seleni ...
- oracle高级查询练习题
1. 列出员工表中每个部门的员工数和部门编号 Select deptno,count(*) from emp group by deptno; 补充1:列出员工表中,员工人数大于3的部门编号和员工人 ...
- IDEA小技巧:添加代码快捷方式
非常怀恋eclipse的的代码快捷方式tryc,今天给IDEA也添加了一个
- sql多表链接之三表连接查询
表与表之间的关系如下 查询条件:根据员工表的enployee_id 查找他在哪个部门,他在哪个城市工作. 查询语句:
- 【Android学习】Android编码规范
四种常见的命名法 比较Java和c#的命名规范的不同点 常量用大写 java方法首字母不大写,应该小写 函数行数限制 不要用拼音 参照物,Android源码 看源码工具,SourceInsight 和 ...
- 策略与计费控制(PCC)流程与信令流程
该文为3GPP TS23.203-be0 条款6-7译文 策略与计费控制(PCC)流程[^4] IP-CAN 会话有三种显著的场景: 无网关控制会话需求,不会出现网关控制建立 需要网关控制会话支持:B ...
- jQuery bind() live()
<script type="text/javascript"> $(document).ready(function () { /*$('.clickme').live ...
- URLRewrite 实现方法详解
所谓的伪静态页面,就是指的URL重写,在ASP.NET中实现非常简单首先你要在你的项目里引用两个DLL:ActionlessForm.dll.URLRewriter.dll,真正实现重写的是 URLR ...
- mysql 字符串的截取与连接
mysql 字符串的截取 left() .right() . substring()[ mid() .substr() 等价于substring() ] .substring_index() l ...
- Bitnami WordPress如何修改MySQL root的默认密码?
Bitnami WordPress安装完毕后,MySQL root的默认密码为空,我们应该马上修改MySQL密码,在开始菜单里面,进入Bitnami ,启动控制台程序,随后输入: mysql -u r ...