【LeetCode】946. Validate Stack Sequences 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/minimum-increment-to-make-array-unique/description/
题目描述
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 <= 10000 <= pushed[i], popped[i] < 1000pushedis a permutation ofpopped.pushedandpoppedhave distinct values.
题目大意
给出了一个栈的输入数字,按照这个顺序输入到栈里,能不能得到一个对应的栈的输出序列。
解题方法
模拟过程
我使用的方法异常简单粗暴,直接模拟这个过程。
题目已知所有的数字都是不同的。我们在模拟这个弹出的过程中,进行一个判断,如果这个弹出的数字和栈顶数字是吻合的,那么栈就要把已有的数字弹出来。如果栈是空的,或者栈顶数字和弹出的数字不等,那么我们应该把pushed数字一直往里放,直到相等为止。
最后,如果栈的入栈序列能得到这个出栈序列,那么栈应该是空的。
class Solution(object):
def validateStackSequences(self, pushed, popped):
"""
:type pushed: List[int]
:type popped: List[int]
:rtype: bool
"""
stack = []
N = len(pushed)
pi = 0
for i in range(N):
if stack and popped[i] == stack[-1]:
stack.pop()
else:
while pi < N and pushed[pi] != popped[i]:
stack.append(pushed[pi])
pi += 1
pi += 1
return not stack
使用C++代码如下,思路和上面一样,其实可以简化代码。2018 年 12 月 7 日 —— 恩,12月又过去一周了
class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
bool canbe = true;
int N = pushed.size();
stack<int> s;
int pi = 0;
for (int i = 0; i < N; i++) {
s.push(pushed[i]);
while (!s.empty() && s.top() == popped[pi]) {
s.pop();
pi++;
}
}
return s.empty();
}
};
日期
2018 年 11 月 24 日 —— 周日开始!一周就过去了~
2018 年 12 月 7 日 —— 恩,12月又过去一周了
【LeetCode】946. Validate Stack Sequences 解题报告(Python & C++)的更多相关文章
- Leetcode 946. Validate Stack Sequences 验证栈序列
946. Validate Stack Sequences 题目描述 Given two sequences pushed and popped with distinct values, retur ...
- (栈)leetcode 946. Validate Stack Sequences
Given two sequences pushed and popped with distinct values, return true if and only if this could ha ...
- 946. Validate Stack Sequences
946. Validate Stack Sequences class Solution { public: bool validateStackSequences(vector<int> ...
- 【leetcode】946. Validate Stack Sequences
题目如下: Given two sequences pushed and popped with distinct values, return true if and only if this co ...
- 【LeetCode】Repeated DNA Sequences 解题报告
[题目] All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...
- 946. Validate Stack Sequences验证栈序列
网址:https://leetcode.com/problems/validate-stack-sequences/ 参考:https://leetcode.com/problems/validate ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- LeetCode 946. 验证栈序列(Validate Stack Sequences) 26
946. 验证栈序列 946. Validate Stack Sequences 题目描述 Given two sequences pushed and popped with distinct va ...
- 【LeetCode】468. Validate IP Address 解题报告(Python)
[LeetCode]468. Validate IP Address 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
随机推荐
- C语言计算fastq文件GC含量2
改进了一下,利用zlib可以读取gz格式的压缩文件,也可以直接计算非压缩格式 #include <stdio.h> #include <stdlib.h> #include & ...
- 利用vcftools比较两个vcf文件
因为最近有一项工作是比较填充准确性的,中间有用到vcftools比较两个vcf文件. 使用命令也很简单: 1 vcftools --vcf file1.snp.vcf --diff file2.snp ...
- 巩固javaweb第十一天
巩固内容: HTML <script> 元素 <script>标签用于加载脚本文件,如: JavaScript. <script> 元素在以后的章节中会详细描述. ...
- Shell 输出第五行的内容
目录 Shell 输出第五行的内容 题目 题解-awk 题解-sed Shell 输出第五行的内容 题目 写一个 bash脚本以输出一个文本文件 nowcoder.txt 中第5行的内容. 示例: 假 ...
- Java日期格式转换不用发愁
前言 Java 中日期.时间相关的类相当的多,并且分不同的版本提供了不同的实现,包括 Date . Calendar . LocalDateTime . ZoneDateTime . OffsetDa ...
- 大厂高频面试题Spring Bean生命周期最详解
Spring作为当前Java最流行.最强大的轻量级框架.Spring Bean的生命周期也是面试高频题,了解Spring Bean周期也能更好地帮助我们解决日常开发中的问题.程序员应该都知道Sprin ...
- 大数据学习day16------第三阶段-----scala04--------1. 模式匹配和样例类 2 Akka通信框架
1. 模式匹配和样例类 Scala有一个十分强大的模式匹配机制,可以应用到很多场合:如switch语句.类型检查等.并且Scala还提供了样例类,对模式匹配进行了优化,可以快速进行匹配 1.1 模式匹 ...
- 前端页面存放token
//本地缓存,记录token function set(type, value) { localStorage.setItem(type, value); } function get(type) { ...
- C++自定义字符串类
//header.h #ifndef _HEADER_H #define _HEADER_H #define defaultSize 128 #include<iostream> #inc ...
- 【Python】【Basic】【数据类型】运算符与深浅拷贝
运算符 1.算数运算: 2.比较运算: 3.赋值运算: 4.逻辑运算: 5.成员运算: 三元运算 三元运算(三目运算),是对简单的条件语句的缩写. # 书写格式 result = 值1 if 条件 ...