11 2023 档案
摘要:123.买卖股票的最佳时机III class Solution: def maxProfit(self, prices: List[int]) -> int: if len(prices) <= 1: return 0 if len(prices) == 2: return max(0, price
阅读全文
摘要:121. 买卖股票的最佳时机 1、动态规划 class Solution: def maxProfit(self, prices: List[int]) -> int: # dp[i][0] 代表第 i 天持有股票获取的最大利益 # dp[i][1] 代表第 i 天不持有股票获取的最大利益 dp =
阅读全文
摘要:198.打家劫舍 1、动态规划 class Solution: def rob(self, nums: List[int]) -> int: # dp 数组代表在第 i 个房间可以偷窃到的最高金额为 dp[i] dp = [0] * len(nums) if len(nums) == 1: retu
阅读全文
摘要:139.单词拆分 class Solution: def wordBreak(self, s: str, wordDict: List[str]) -> bool: dp = [False] * (len(s) + 1) dp[0] = True # 求排列先遍历背包再遍历物品 for i in r
阅读全文
摘要:70. 爬楼梯 (进阶) 1、使用 01 背包解法 class Solution: def climbStairs(self, n: int) -> int: # dp 数组代表爬上第 i 阶有 dp[j] 种方法 dp = [0] * (n + 1) dp[0] = 1 m = 2 # 排列先背包
阅读全文
摘要:[完全背包] 有N件物品和一个最多能背重量为W的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。每件物品都有无限个(也就是可以放入背包多次),求解将哪些物品装入背包里物品价值总和最大。 1、先遍历物品再遍历背包 def all_bag(weight, value, bag
阅读全文
摘要:1049.最后一块石头的重量II class Solution: def lastStoneWeightII(self, stones: List[int]) -> int: total = sum(stones) target = total // 2 dp = [0] * (target + 1
阅读全文
摘要:[背包基础] 题目:有n件物品和一个最多能背重量为w 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。每件物品只能用一次,求解将哪些物品装入背包里物品价值总和最大。 1、二维方式解决背包问题 class Solution: def solve_bag(self, wei
阅读全文
摘要:96.不同的二叉搜索树 1、关键点找出状态转移方程 class Solution: def numTrees(self, n: int) -> int: # 创建 dp 数组, dp[i] 代表节点数为 i 的二叉搜索树数量 dp = [0]*(n+1) # 初始化数组 dp[0] = 1 # 遍历
阅读全文
摘要:62.不同路径 class Solution: def uniquePaths(self, m: int, n: int) -> int: # dp[i][j] 代表到达 dp[i][j] 有多少不同路径 dp = [[0]*n for _ in range(m)] # 初始化 for i in r
阅读全文
摘要:509. 斐波那契数 1、动态规划 class Solution: def fib(self, n: int) -> int: if n <= 1: return n # dp[i] 代表第 i 个数的斐波那契值 dp = [0] * (n+1) dp[0] = 0 dp[1] = 1 for i
阅读全文
摘要:738.单调递增的数字 class Solution: def monotoneIncreasingDigits(self, n: int) -> int: # 主要思路当前数字比前面数字小时。前面数字 -1,当前数字变2为 9 str_n = str(n) for i in range(len(s
阅读全文
摘要:435. 无重叠区间 class Solution: def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: intervals.sort(key=lambda x: x[0]) count = 0 for i in r
阅读全文
摘要:860.柠檬水找零 class Solution: def lemonadeChange(self, bills: List[int]) -> bool: five, ten, twenty = 0, 0, 0 for bill in bills: if bill == 5: five += 1 e
阅读全文
摘要:1005.K次取反后最大化的数组和 class Solution: def largestSumAfterKNegations(self, nums: List[int], k: int) -> int: nums.sort(key=lambda x:abs(x), reverse=True) fo
阅读全文
摘要:122.买卖股票的最佳时机 II 1、贪心 class Solution: def maxProfit(self, prices: List[int]) -> int: res = 0 for i in range(1, len(prices)): res += max(prices[i]-pric
阅读全文
摘要:455.分发饼干 1、优先大饼干 class Solution: def findContentChildren(self, g: List[int], s: List[int]) -> int: g.sort() s.sort() index = len(s) - 1 # 最后一块饼干 res =
阅读全文
摘要:332.重新安排行程 方法一和方法二在力扣用例会超时 方法一、 class Solution: def findItinerary(self, tickets: List[List[str]]) -> List[str]: tickets.sort() res = [] used = [False]
阅读全文
摘要:491.递增子序列 class Solution: def findSubsequences(self, nums: List[int]) -> List[List[int]]: res = [] self.tracebacking(nums, 0, [], res) return res def
阅读全文
摘要:93.复原IP地址 1、方法一 class Solution: def restoreIpAddresses(self, s: str) -> List[str]: res = [] self.tracebacking(s, 0, [], res) return res def tracebacki
阅读全文
摘要:39. 组合总和 class Solution: def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: res = [] candidates.sort() self.tracebacking
阅读全文
摘要:216.组合总和III class Solution: def combinationSum3(self, k: int, n: int) -> List[List[int]]: res = [] self.tracebacking(n, k, 1, 0, [], res) return res d
阅读全文
摘要:第77题. 组合 需要注意剪枝细节 纵向代表递归,横行代表取数 1、回溯 class Solution: def combine(self, n: int, k: int) -> List[List[int]]: res = [] self.backtrack(n, k, 1, [], res) r
阅读全文
摘要:669. 修剪二叉搜索树 树的修剪方式赋值。 1、递归法 class Solution: def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]: if root is None:
阅读全文
摘要:235. 二叉搜索树的最近公共祖先 关键点:最近公共祖先的判断,二叉树的特性 1、做二叉树的模式 class Solution: def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'Tr
阅读全文