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

基于ROS与Gazebo的多机器人三角形编队仿真实践 本文详细介绍了基于ROS与Gazebo的多机器人三角形编队仿真实践,从环境搭建到编队控制的全过程。通过TurtleBot3机器人模型和TF坐标系系统,实现三个机器人的三角形编队控制,适用于机器人协同作业与编队算法研究。 阅读详情

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);
    }
第10篇:Agent + 爬虫 = 全自动数据采集专家 在企业级数据采集场景中,传统爬虫的痛点已经到了无法忽视的地步:而Agent + 爬虫的组合,就是这些痛点的终极解决方案。它的核心逻辑是:把爬虫从「人工写死的固定执行脚本」,升级为「能自主分析、自主决策、自主适配、自主修复的智能采集机器人」。我们在生产环境中做过对比测试:传统爬虫开发10个网站的采集脚本需要3人天,而Agent采集机器人只需要3分钟,页面改版后的修复成本从1人天降到了0,采集任务的成功率从62%提升到了95%以上。终端执行以下命令,安装基础依赖,与前9篇环境完全兼容: 二、核心能力拆解:Age 阅读详情

相关推荐

S32DS使用手把手教程:从零实现CAN通信配置

深入讲解S32DS使用过程中如何从零开始配置CAN通信,涵盖关键步骤与实用技巧,帮助开发者快速上手S32DS使用中的常见问题与调试方法。

weixin_42405592的博客 1145

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

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

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

Aurix TC387 Ethernet代码解析之二_移植

英飞凌官方仅提供了TC397的例程,故需通过TC397例程来移植到TC387新建工程里。TC397官方例程名称【Ethernet_1_KIT_TC397_TFT】,网络上可以免费获取到,此处不赘述。使用工具【

m0_69929716的博客 901

946. Validate Stack Sequences

946. Validate Stack Sequences

weixin_49991368的博客 191

银河麒麟桌面版V10搭建Qt4.8.6开发环境

银河麒麟桌面版V10搭建Qt4.8.6开发环境

qq_34932364的博客 2408

TOD和PPS精确时间同步技术

授时

u010783226的专栏 1万+

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

ZYNQ:【1】深入理解PS端的TTC定时器(Part1:原理+官方案例讲解)

基于UG585详细讲解其中TTC的原理和使用案例。

Alex的技术博客 7910

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

m0_37889928的博客 298

从BIN文件到可烧录程序:深入理解ADI DSP程序读取背后的原理与安全边界

本文深入探讨了ADI DSP程序从BIN文件到可烧录程序的转换原理与安全边界,详细解析了JTAG接口、仿真器工作流程及Flash存储结构。通过Visual DSP++工具,开发者可以理解程序读取的底层机制,同时强调了知识产权保护的重要性,为嵌入式开发提供了实用建议。

weixin_27956639的博客 280

GAN网络(论文阅读):Generative Adversarial Nets

当判别器效果比较好时,D(G(z))值为0,则log(1-D(G(z)))值为0,当判别器效果不好,会输出一个大于0 的数,则log(1-D(G(z)))值就为一个负数,在极端情况下,会得到负无穷大,此时判别失误。在价值函数的第二项中存在问题,在早期G比较弱的时,生成的数据和真实数据相差很大,D就容易训练的比较好,很容易区分,就会导致log(1-D(G(z)))变成0 ,这样很难求梯度来优化G,所以更新G时,将优化函数改为log(D(G(z)))。最终使G赢,即G生成的数据D判别不出来。

m0_54248968的博客 2299

LC.946. Validate Stack Sequences

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

很吵请安静 412

Apifox接口测试+自动化测试

Apifox 接口测试+自动化测试快速入门!

程序员小杨哥的博客 1万+

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

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

【小白爬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的博客 292

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

第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

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

【Leetcode】946. Validate Stack Sequences

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

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

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的博客 565

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

拾遗 651

[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 689

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的博客 247

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的博客 205

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的博客 268
上一篇: 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、付费专栏及课程。

余额充值
⌂ 换网址