您现在的位置是:亿华云 > 系统运维
聊聊栈的压入、弹出序列
亿华云2025-10-03 09:12:00【系统运维】6人已围观
简介本文转载自微信公众号「程序员千羽」,作者程序员千羽 。转载本文请联系程序员千羽公众号。Leetcode : https://leetcode-cn.com/problems/zhan-de-ya-ru
本文转载自微信公众号「程序员千羽」,聊聊作者程序员千羽 。压入转载本文请联系程序员千羽公众号。弹出
Leetcode : https://leetcode-cn.com/problems/zhan-de-ya-ru-dan-chu-xu-lie-lcof
“GitHub : https://gitee.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_24_validateStackSequences/Solution.java
栈的序列压入、弹出序列
“题目描述 :输入两个整数序列,聊聊第一个序列表示栈的压入压入顺序,请判断第二个序列是弹出否为该栈的弹出顺序。假设压入栈的序列所有数字均不相等。例如,聊聊序列 { 1,压入2,3,4,5} 是某栈的压栈序列,序列 { 4,弹出5,3,2,1} 是该压栈序列对应的一个弹出序列,但 { 4,序列3,5,1,2} 就不可能是该压栈序列的弹出序列。示例:
输入: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 <= 1000
0 <= pushed[i],压入 popped[i] < 1000
pushed 是 popped 的排列。
解题思路: 用栈来压入弹出元素,弹出相等则出栈。
如下图所示,给定一个压入序列 pushed和弹出序列 popped,则压入 / 弹出操作的顺序(即排列)是香港云服务器 唯一确定 的。
如下图所示,栈的数据操作具有 先入后出 的特性,因此某些弹出序列是无法实现的。
考虑借用一个辅助栈stack,模拟压入1弹出操作的排列。根据是否模拟成功,即可得到结果。
入栈操作:按照压栈序列的顺序执行 出栈操作:每次入栈后,循环判断“栈顶元素=弹出序列的当前元素”是否成立,将符合弹出序列顺序 的栈顶元素全部弹出。由于题目规定|栈的所有数字均不相等, 因此在循环入栈中,每个元素出栈的位置的可能性是唯一的(若有重复数字,则具有多个可出栈的位置)。因而, 在遇到“栈顶元素=弹出序列的服务器租用当前元素”就应立即执行出栈。
算法流程: 初始化:辅助栈stack,弹出序列的索引i ; 遍历压栈序列:各元素记为num ; 元愫num入栈; 循环出栈:若stack的栈顶元素=弹出序列元素popped[i],则执行出栈与i++ ;返回值:若stack为倥,则此弹出序列合法。
复杂度分析:
时间复杂度O(N): 其中N为列表pushed的长度;每个元素最多入栈与出栈一次,即最多共2N次出入栈操作。 空间复杂度O(N): 辅助栈stack最多同时存储N个元素。 package com.nateshao.sword_offer.topic_24_validateStackSequences; import java.util.Stack; /** * @date Created by 邵桐杰 on 2021/11/28 21:53 * @微信公众号 程序员千羽 * @个人网站 www.nateshao.cn * @博客 https://nateshao.gitee.io * @GitHub https://github.com/nateshao * @Gitee https://gitee.com/nateshao * Description: 栈的压入、弹出序列 * 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。 * 假设压入栈的所有数字均不相等。例如,序列 { 1,2,3,4,5} 是某栈的压栈序列, * 序列 { 4,5,3,2,1} 是该压栈序列对应的一个弹出序列,但 { 4,3,5,1,2} 就不可能是该压栈序列的弹出序列。 * https://leetcode-cn.com/problems/zhan-de-ya-ru-dan-chu-xu-lie-lcof */ public class Solution { public static void main(String[] args) { int[] pushed = { 1, 2, 3, 4, 5}; int[] popped = { 4, 5, 3, 2, 1}; int[] popped1 = { 4, 3, 5, 1, 2}; boolean b = validateStackSequences1(pushed, popped); System.out.println("b = " + b);// b = true boolean b1 = validateStackSequences2(pushed, popped1); System.out.println("b1 = " + b1); } /** * 精选解答 * * @param pushed * @param popped * @return */ public static boolean validateStackSequences1(int[] pushed, int[] popped) { if (pushed == null || pushed == null) return false; Stack<Integer> stack = new Stack<>(); int index = 0; for (int num : pushed) { stack.push(num); while (!stack.isEmpty() && stack.peek() == popped[index]) { stack.pop(); index++; } } return stack.isEmpty(); } /** * 思路:用栈来压入弹出元素,相等则出栈。 * * @param pushed * @param popped * @return */ public static boolean validateStackSequences2(int[] pushed, int[] popped) { if (pushed == null || popped == null) return false; Stack<Integer> stack = new Stack<>();// 借用一个辅助栈stack int index = 0; for (int i = 0; i < pushed.length; i++) { stack.push(pushed[i]);// 入栈 //循环判断 栈不为空 && 栈顶元素 == 弹出序列的亿华云计算当前元素 while (!stack.isEmpty() && stack.peek().equals(popped[index])) { stack.pop(); index++; } } return stack.isEmpty(); // return index==popped.length; } }参考链接:https://leetcode-cn.com/problems/zhan-de-ya-ru-dan-chu-xu-lie-lcof/solution/mian-shi-ti-31-zhan-de-ya-ru-dan-chu-xu-lie-mo-n-2
很赞哦!(4982)
上一篇: 评估新的数据中心计算范式