LeetCode 126题:单词接龙 II

题目描述 给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出所有从 beginWord 到 endWord 的最短转换序列的转换过程,转换需遵循如下规则: 每次转换只能改变一个字母。转换过程中的中间单词必须在字典 wordList 中。 说明: 如果不存在这样的转换序列,返回一个空列表。所有单词具有相同的长度。所有单词只包含小写字母。字典 wordList 是非空的,且不包...

LeetCode 力扣题目:买卖股票的最佳时机 III

题目描述 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。 设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。 注意: 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。 示例: 输入: [3,3,5,0,0,3,1,4] 输出: 6 解释: 在第 4 天(股价 = 0)的时候买入,在第 6 天(股价 = 3)的时候卖出,这笔交易所能获得利润 = 3-0 = 3。 ...

LeetCode:2391. 收集垃圾的最少总时间 + 二分】

🍔 目录 🚩 题目链接⛲ 题目描述🌟 求解思路&实现代码&运行结果⚡ 二分🥦 求解思路🥦 实现代码🥦 运行结果 💬 共勉 🚩 题目链接 2391. 收集垃圾的最少总时间 ⛲ 题目描述 给你一个下标从 0 开始的字符串数组 garbage ,其中 garbage[i] 表示第 i 个房子的垃圾集合。garbage[i] 只包含字符 ‘M’ ,‘P’ 和 ‘G’ ,但可能包含多个相同字符,每个字符分别表示一单位的金...

LeetCode //C - 81. Search in Rotated Sorted Array II

ed at some pivot. − 1 0 4 < = t a r g e t < = 1 0 4 -10^4 <= target <= 10^4 −104<=target<=104 From: LeetCode Link: 81. Search in Rotated Sorted Array II Solution: Ideas: Initialization: We start with two poin...

LeetCode题目104: 二叉树的最大深度(递归\迭代\层序遍历\尾递归优化\分治法实现 )

题目描述 给定一个二叉树,找出其最大深度。 最大深度是从根节点到最远叶子节点的最长路径上的节点数。 示例: 给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 最大深度是 3。 方法一:递归 解题步骤 如果节点为空,返回深度 0。递归计算左子树的最大深度。递归计算右子树的最大深度。返回左右子树深度的最大值加一(当前节点的深度)。 Python 示例 class ...

LeetCode 题目 120:三角形最小路径和

题目描述 给定一个三角形,找出自顶向下的最小路径和。每一步只能移动到下一行中相邻的结点上。相邻的结点在这里指的是下一行中与这个结点正下方或者正下方右边的结点。 方法一:动态规划(自底向上) 解题步骤: 从三角形的最后一行开始,用一个数组 dp 存储到当前行每个元素的最小路径和。对于三角形的每一行,更新 dp 数组中的每个值,使其等于当前元素加上它下面行中相邻元素的较小者。最终,dp 数组的第一个元素将包含从顶部...

LeetCode //C - 47. Permutations II

in any order.   Example 1: Example 2: Constraints: 1 <= nums.length <= 8-10 <= nums[i] <= 10 From: LeetCode Link: 47. Permutations II Solution: Ideas: 1. Sorting the Array: The function starts by sorting the...

LeetCode //C - 7. Reverse Integer

: Example 3: Constraints: − 2 31 < = x < = 2 31 − 1 -2^{31} <= x <= 2^{31} - 1 −231<=x<=231−1 From: LeetCode Link: 7. Reverse Integer Solution: Ideas: 1. Initialize a result variable (reversed) to zero: This ...

LeetCode //C - 60. Permutation Sequence

permutation sequence.   Example 1: Example 2: Example 3: Constraints: 1 <= n <= 91 <= k <= n! From: LeetCode Link: 60. Permutation Sequence Solution: Ideas: Factorial Calculation: The function starts by calcu...

LeetCode //C - 87. Scramble String

ts: s1.length == s2.length1 <= s1.length <= 30s1 and s2 consist of lowercase English letters. From: LeetCode Link: 87. Scramble String Solution: Ideas: 1. Recursive Definition: We want to define a function is...
© 2024 LMLPHP 关于我们 联系我们 友情链接 耗时0.016213(s)
2024-05-19 19:46:55 1716119215