首页 > 其他分享 >Study Plan For Algorithms - Part25

Study Plan For Algorithms - Part25

时间:2024-09-07 11:52:34浏览次数:7  
标签:popped 压入 Study len pushed while Algorithms Plan stack

1. 栈的压入、弹出序列
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。
方法一:

def validateStackSequences(pushed, popped):
    stack = []
    len_pushed = len(pushed)
    stack_index = 0
    i = 0
    while i < len_pushed:
        stack.append(pushed[i])
        while stack and stack[-1] == popped[stack_index]:
            stack.pop()
            stack_index += 1
        i += 1
    return not stack

方法二:

def validateStackSequences(pushed, popped):
    stack = []
    i = 0
    j = 0

    while i < len(pushed):
        stack.append(pushed[i])
        i += 1

        while stack and stack[-1] == popped[j]:
            stack.pop()
            j += 1

    return len(stack) == 0

方法三:

def validateStackSequences(pushed, popped):
    stack = []
    j = 0

    for num in pushed:
        stack.append(num)
        while stack and stack[-1] == popped[j]:
            stack.pop()
            j += 1

    return j == len(popped)

标签:popped,压入,Study,len,pushed,while,Algorithms,Plan,stack
From: https://blog.csdn.net/qq_24058289/article/details/141993553

相关文章

  • Study Plan For Algorithms - Part23
    1.跳跃游戏II题目链接:https://leetcode.cn/problems/jump-game-ii/给定一个长度为n的0索引整数数组nums。初始位置为nums[0]。每个元素nums[i]表示从索引i向前跳转的最大长度:0<=j<=nums[i]i+j<n返回到达nums[n-1]的最小跳跃次数。classSolutio......
  • Study Plan For Algorithms - Part22
    1.字符串相乘题目链接:https://leetcode.cn/problems/multiply-strings/给定两个以字符串形式表示的非负整数num1和num2,返回num1和num2的乘积,它们的乘积也表示为字符串形式。classSolution:defmultiply(self,num1:str,num2:str)->str:ifnum1==......
  • Study Plan For Algorithms - Part21
    1.缺失的第一个正数题目链接:https://leetcode.cn/problems/first-missing-positive/给定一个未排序的整数数组nums,请找出其中没有出现的最小的正整数。classSolution:deffirstMissingPositive(self,nums:List[int])->int:n=len(nums)forii......
  • Study Plan For Algorithms - Part20
    1.组合总和题目链接:https://leetcode.cn/problems/combination-sum/给定一个无重复元素的整数数组candidates和一个目标整数target,找出candidates中可以使数字和为目标数target的所有不同组合,并以列表形式返回。classSolution:defcombinationSum(self,ca......
  • ROS2- Moveit2 - 运动规划API(Motion Planning API)
     在MoveIt中,运动规划器使用插件基础结构加载。这允许MoveIt在运行时加载运动规划器。在此示例中,我们将运行执行此操作所需的C++代码。入门如果您还没有这样做,请确保您已经完成入门指南中的步骤。 运行演示打开两个shell。在第一个shell中启动RViz并等待所有内......
  • ROS2- Moveit2 -Planning Scene ROS API (规划场景 ROS API)
    在本教程中,我们将研究如何使用规划场景差异来执行两项操作:在世界中添加和移除物体将物体安装到机器人上或从机器人上卸下入门如果您还没有这样做,请确保您已经完成入门指南中的步骤。运行代码打开两个shell。在第一个shell中启动RViz并等待所有内容完成加载:ros......
  • Study Plan For Algorithms - Part20
    1.树的子结构输入两棵二叉树A和B,判断B是不是A的子结构。方法一:classTreeNode:def__init__(self,val=0,left=None,right=None):self.val=valself.left=leftself.right=rightdefisSubStructure(A,B):ifnotAornot......
  • Ros2-Moveit2-PlanningSceneMonitor(规划场景监控)
    PlanningSceneMonitor是维护最新规划场景的推荐接口。RobotState、CurrentStateMonitor、PlanningScene、PlanningSceneMonitor和PlanningSceneInterface之间的关系一开始可能非常令人困惑。本教程旨在阐明这些关键概念。机器人状态RobotState是机器人的快照。它包含RobotMod......
  • Study Plan For Algorithms - Part18
    1.搜索插入位置题目链接:https://leetcode.cn/problems/search-insert-position/给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。classSolution:defsearchInsert(self,nums:List[int],target:......
  • COMP20003 Algorithms and Data Structures Spellcheck Lookup
    Assignment2:SpellcheckLookupGeneralYoumustreadfullyandcarefullytheassignmentspecificationandinstructions.Course:COMP20003AlgorithmsandDataStructures@Semester2,2024DeadlineSubmission:Friday6thSeptember2024@11:59pm(endo......