leetcode 946. Validate Stack Sequences(有效栈序列)

intel-VROC raid卡驱动 centos7.9 u盘进入系统,按e,在出现下面在quiet前面输入inst.dd initcall_blacklist=vmd_drv_init 后按Ctrl和x,开始引导。进入系统,按e,在出现下面在quiet前面输入inst.dd inicll_blacklist=vmd_drv_init 后按Ctrl和x,开始引导。驱动完成后,在输入 c 回车, 引导过程开始,进入安装界面。输入1 ,选择这个ISO驱动文件里的rpm包。输入1 ,选择这个ISO驱动文件里的rpm包。vi编辑 /etc/default/grub。 阅读详情

Given two integer arrays pushed and popped each with distinct values, return true if this could have been the result of a sequence of push and pop operations on an initially empty stack, or false otherwise.

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.

Constraints:

1 <= pushed.length <= 1000
0 <= pushed[i] <= 1000
All the elements of pushed are unique.
popped.length == pushed.length
popped is a permutation of pushed.

给两个数组,一个是要压栈的数字,一个是要出栈的数字。问经过这些出栈操作之后栈是否能恢复空。
即push, pop操作是否有效。

思路:
可用两个指针顺序指向pushed, popped中的数字,两个一样时直接跳过(不用先push再pop一遍),
不一样时先看stack中有没有可以pop的元素(即栈顶元素是否与pop元素相等),如果有,pop,然后pop指针右移;
如果没有, push入stack中,push指针右移(但是如果push已经到最右端,就返回挨个找栈顶是否有匹配元素,如果和栈顶不匹配,直接返回false)

//3ms
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        int n = pushed.length;
        Stack<Integer> stack = new Stack<>();
        int push = 0;
        int pop = 0;
        
        while(pop < n) {
            if(push >= n) {
                if(stack.isEmpty() || stack.peek() != popped[pop]) return false;
                else stack.pop();
                pop ++;
            } else {
                if(pushed[push] != popped[pop]) {
                    if(!stack.isEmpty() && stack.peek() == popped[pop]) {
                        stack.pop();
                        pop ++;
                    } else {
                       stack.push(pushed[push]);
                       push ++; 
                    }
                
                } else {
                    push ++;
                    pop ++;
               
                }
                
            }
            
            
        }
        return true;
    }

或者是每次都push, 然后先看stack中有没有可以pop的,一下pop完,再进入下一个push。

//4ms
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        int n = pushed.length;
        Stack<Integer> stack = new Stack<>();
        int pop = 0;
        
        for(int num : pushed) {
            stack.push(num);
            while(!stack.isEmpty() && stack.peek() == popped[pop]) {
                stack.pop();
                pop ++;
            }
        }
        return stack.isEmpty();
    }

数组模拟stack

//0ms
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        int n = pushed.length;
        int[] st = new int[n];
        int head = -1; //栈顶
        int pushP = 0;  //pushed pointer
        int popP = 0;   //popped pointer

        for(int pushNum : pushed) {
            st[++head] = pushed[pushP++];
            while(head >= 0 && st[head] == popped[popP]) {
                head --;
                popP ++;
            }
        }
        return (head == -1);
    }
深度剖析:Spring AI 与 LangChain4j,谁才是 Java 程序员的 AI 开发利器? 首先介绍 AI 时代 Java 开发框架选择背景,接着对比两者功能特性(Spring AI 的生态集成与标准化设计、LangChain4j 的多模型适配与提示工程)、应用场景(企业级系统改造与创新产品开发)、开发体验(学习成本与调试工具)、生态扩展(Spring 商业支持与 LangChain4j 开源社区)、性能安全(企业级优化与自主安全实现)及成本趋势(短期投入、长期回报与技术演进),最后给出选型建议与混合架构方案。 阅读详情

相关推荐

Mac上Ollama服务IP访问失败的3种修复方法(含永久生效配置)

本文详细介绍了Mac上Ollama服务IP访问失败的3种修复方法,包括临时环境变量覆盖、永久Shell配置修改及进阶排查技巧。重点解析了OLLAMA_HOST环境变量的配置原理,帮助开发者解决Connection refused问题,实现局域网IP访问Ollama服务的需求。

weixin_29209805的博客 206

【剑指offer】31.栈的压入、弹出序列

31.栈的压入、弹出序列 面试题31. 栈的压入、弹出序列 难度中等27 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如,序列 {1,2,3,4,5} 是某栈的压栈序列,序列 {4,5,3,2,1} 是该压栈序列对应的一个弹出序列,但 {4,3,5,1,2} 就不可能是该压栈序列的弹出序列。 示例 1: 输入:pushed ...

芝兰生于深谷,不以无人而不芳 868

FX2N-2AD模块实战:从硬件连接到FROM/TO指令编程详解

本文详细解析FX2N-2AD模拟量输入模块的硬件连接与FROM/TO指令编程技巧,涵盖BFM缓冲存储器配置、梯形图程序设计及工程实战经验。通过实例演示如何实现精准信号采集与数据处理,特别针对PLC系统中的常见故障提供解决方案,助力工业自动化工程师高效完成设备调试与维护。

prometheus9mon的博客 505

946. Validate Stack Sequences

946. Validate Stack Sequences

weixin_49991368的博客 191

全能扫描王的实现(python版本)- 目标检测图像矫正

全能扫描王python代码实现,可用于文件扫描或者是图像校正

搬砖猛女的啃书历程 5487

【语音识别算法】深度学习语音识别算法与传统语音识别算法的区别、对比及联系

深度学习语音识别算法与传统语音识别算法在理论基础、实现方式、性能表现等方面存在显著区别,同时也有一些联系。下面将从几个方面详细比较这两种方法,并给出应用实例和代码示例。

weixin_51306394的博客 2566

LC.946. Validate Stack Sequences

class Solution(object): def validateStackSequences(self, pushed, popped): """ 依次遍历poped中的每一个元素,将pushed中对应的元素压到栈顶再弹出,如果无法完成此操作则终止循环 """ stack = [] push_inde...

很吵请安静 412

混合开关电容DCDC变换器技术解析与工程应用指南

在电源设计领域,传统DCDC变换器在高降压比或高升压比场景下,往往面临占空比过窄、开关应力大、效率偏低等瓶颈。开关电容变换器虽能实现固定比例的电压变换,却受限于调节能力。混合开关电容DCDC变换器通过“开关电容网络粗调变比+电感式DCDC细调输出”的策略,兼顾高效率、高功率密度与宽调压范围。本文梳理了其典型拓扑(如2:1/4:1 SC+Buck)、关键对比、控制策略(含软充电)及工程应用中的常见问题,帮助电源工程师在服务器供电、快充、车载电源等场景下快速选型与调试。

weixin_29164185的博客 311

栈的弹出序列问题

给定栈的压入序列判断栈的弹出序列是否合法 附上动图,一看就会 代码在此: class Solution { public boolean validateStackSequences(int[] pushed, int[] popped) { Stack<Integer> stack =new Stack<>(); int i =...

CodeHunter_qcy 553

CloudCompare如何进行二次开发?

CloudCompare源代码编译成功后,即可进行二次开发,可以通过修改源码或者制作插件实现二次开发。本文二次开发采用修改CloudCompare的源代码的方式,分别修改mainwindow.ui、mainwindow.h、mainwindow.cpp,实现扩展功能,克隆选中的点云。

【cacrle】的博客 5331

【FISCO-BCOS】 八、ubuntu配置Java环境

ubuntu配置Java环境需根据自己的java版本号进行细节修改,本文以jdk11.0.18版本为例。ps:通过此法还可以查看java安装路径,但必须是环境配置成功的情况下才能使用。二、添加Java环境路径。四、检查java环境设置。

naihe_fish的博客 4307

LeetCode 946. 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...

wenmiao_的博客 351

【Davinci开发】:MPU(内存保护)

【Davinci开发】:MPU(内存保护)

2205

Leetcode 946. Validate Stack Sequences 验证栈序列

Leetcode 946. Validate Stack Sequences 验证栈序列946. Validate Stack Sequences题目描述示例示例1示例2解答 946. Validate Stack Sequences 题目描述 Given two sequences pushed and popped with distinct values, return true if an...

importsys的博客 427

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

【小白爬Leetcode946】2.4 验证栈序列 Validate Stack Sequences

【小白爬Leetcode946】2.4 验证栈序列 Validate Stack Sequences题目Description中文描述思路一 用map的实现方法 Leetcode 946 medium\color{#FF6347}{medium}medium 点击进入原题链接:Leetcode946 最小栈 Min Stack 题目 Description Given two sequences pushed and popped with distinct values, return true i

weixin_41604040的博客 290

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

第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)...

Lightmare625 358

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

【Leetcode】946. Validate Stack Sequences

题目地址: https://leetcode.com/problems/validate-stack-sequences/ 给定一个入栈序列,再给定另一个序列,问其是否可能是一个合法的出栈序列。题目保证入栈序列数字彼此不同。 思路是直接用一个栈去模拟。先将入栈序列里的数入栈,然后中途如果发现栈顶等于了出栈序列里的值,则不停出栈。看最后操作完之后栈是否为空,若非空则说明序列不是合法的出栈序列。代码如下: import java.util.ArrayDeque; import java.util.Deque;

数学、算法爱好者的博客 357

LeetCode C++ 946. Validate Stack Sequences【Stack】中等

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,

memcpy0的博客 559

LeetCode 946. Validate Stack Sequences 栈的压入、弹出序列

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)...

拾遗 650

[leetcode]946. Validate Stack Sequences用Stack验证序列

Given two sequencespushedandpoppedwith distinct values,returntrueif and only if this could have been the result of a sequence of push and pop operations on an initially empty stack. Example 1:...

weixin_30781433的博客 141

LeetCode //C - 946. Validate Stack Sequences

Summary: This problem checks if a given popped sequence can be obtained from a pushed sequence using stack operations. We simulate the stack by pushing elements from pushed and popping them when they match the next element in popped. If all elements in pop

Made in Code 683

LeetCode 946: Validate Stack Sequences (栈好题)

946.Validate Stack Sequences Medium Given two sequencespushedandpoppedwith distinct values,returntrueif 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: push...

roufoo的博客 246

946. Validate Stack Sequences验证栈序列

网址:https://leetcode.com/problems/validate-stack-sequences/ 参考:https://leetcode.com/problems/validate-stack-sequences/discuss/197667/Java-straight-forward-stack-solution. 模拟过程 class Solution { pu...

weixin_33704234的博客 204

LeetCode_Stack_946. Validate Stack Sequences 验证栈序列(Java)【栈,模拟】

目录 一,题目描述 英文描述 中文描述 示例与说明 二,解题思路 三,AC代码 Java 四,解题过程 第一博 一,题目描述 英文描述 Given two integer arrays pushed and popped each with distinct values, return true if this could have been the result of a sequence of push and pop operations on an initiall...

&再见萤火虫&的博客 285

Leetcode 946. Validate Stack Sequences 栈的运用

思路 一开始没有思路,多画一画 简单样例,复杂样例 分解样例,连续样例 短期地想,长期地想 最终发现,可以构造一个栈 遍历输入的元素,如果如果刚好能和输出元素对上,就都跳过,反之入栈 最终遍历完成后,栈中元素应当能和剩余输出元素恰好对应上。最后栈空 class Solution { public: bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {.

Xurui_Luo的博客 211

leetcode -- 946. Validate Stack Sequences

题目描述 题目难度:Medium 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 s...

tkzc_csk的博客 267
上一篇: leetcode 71. Simplify Path(简化路径)
下一篇: leetcode 856. Score of Parentheses(括号的分数)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值