From 37e7d73ec200bd9adefbdaaef24d6f4c2e38f5f7 Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Mon, 28 Feb 2022 20:52:01 +0800 Subject: [PATCH] Update --- problems/0020.有效的括号.md | 4 ++-- problems/0027.移除元素.md | 2 +- problems/0035.搜索插入位置.md | 2 +- problems/0039.组合总和.md | 4 ++-- problems/0040.组合总和II.md | 4 ++-- problems/0042.接雨水.md | 2 +- problems/0046.全排列.md | 2 +- problems/0059.螺旋矩阵II.md | 4 ++-- problems/0070.爬楼梯完全背包版本.md | 4 ++-- problems/0077.组合优化.md | 2 +- problems/0078.子集.md | 2 +- problems/0084.柱状图中最大的矩形.md | 4 ++-- problems/0093.复原IP地址.md | 2 +- problems/0102.二叉树的层序遍历.md | 4 ++-- .../0108.将有序数组转换为二叉搜索树.md | 2 +- problems/0110.平衡二叉树.md | 2 +- problems/0129.求根到叶子节点数字之和.md | 2 +- problems/0131.分割回文串.md | 4 ++-- problems/0188.买卖股票的最佳时机IV.md | 2 +- problems/0257.二叉树的所有路径.md | 2 +- problems/0279.完全平方数.md | 2 +- problems/0322.零钱兑换.md | 2 +- problems/0337.打家劫舍III.md | 6 +++--- problems/0376.摆动序列.md | 2 +- problems/0383.赎金信.md | 2 +- problems/0404.左叶子之和.md | 4 ++-- problems/0455.分发饼干.md | 2 +- problems/0463.岛屿的周长.md | 2 +- problems/0474.一和零.md | 2 +- problems/0491.递增子序列.md | 4 ++-- problems/0494.目标和.md | 11 ++++++++--- problems/0496.下一个更大元素I.md | 4 ++-- problems/0501.二叉搜索树中的众数.md | 4 ++-- problems/0503.下一个更大元素II.md | 2 +- problems/0518.零钱兑换II.md | 2 +- problems/0538.把二叉搜索树转换为累加树.md | 2 +- problems/0669.修剪二叉搜索树.md | 2 +- problems/0841.钥匙和房间.md | 1 - ...1047.删除字符串中的所有相邻重复项.md | 4 ++-- problems/二叉树的递归遍历.md | 2 +- problems/周总结/20201003二叉树周末总结.md | 4 ++-- problems/背包问题理论基础完全背包.md | 2 +- 42 files changed, 64 insertions(+), 60 deletions(-) diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md index 95d62e42..01fc7b93 100644 --- a/problems/0020.有效的括号.md +++ b/problems/0020.有效的括号.md @@ -159,7 +159,7 @@ class Solution { ``` Python: -```python3 +```python # 方法一,仅使用栈,更省空间 class Solution: def isValid(self, s: str) -> bool: @@ -180,7 +180,7 @@ class Solution: return True if not stack else False ``` -```python3 +```python # 方法二,使用字典 class Solution: def isValid(self, s: str) -> bool: diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index d69f2bcf..dd9155c6 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -142,7 +142,7 @@ class Solution { Python: -```python3 +```python class Solution: """双指针法 时间复杂度:O(n) diff --git a/problems/0035.搜索插入位置.md b/problems/0035.搜索插入位置.md index f5f041aa..9a770703 100644 --- a/problems/0035.搜索插入位置.md +++ b/problems/0035.搜索插入位置.md @@ -246,7 +246,7 @@ func searchInsert(nums []int, target int) int { ``` ### Python -```python3 +```python class Solution: def searchInsert(self, nums: List[int], target: int) -> int: left, right = 0, len(nums) - 1 diff --git a/problems/0039.组合总和.md b/problems/0039.组合总和.md index 0f8fe4f6..7a2084dd 100644 --- a/problems/0039.组合总和.md +++ b/problems/0039.组合总和.md @@ -264,7 +264,7 @@ class Solution { ## Python **回溯** -```python3 +```python class Solution: def __init__(self): self.path = [] @@ -296,7 +296,7 @@ class Solution: self.path.pop() # 回溯 ``` **剪枝回溯** -```python3 +```python class Solution: def __init__(self): self.path = [] diff --git a/problems/0040.组合总和II.md b/problems/0040.组合总和II.md index 54384188..49acb8d6 100644 --- a/problems/0040.组合总和II.md +++ b/problems/0040.组合总和II.md @@ -334,7 +334,7 @@ class Solution { ## Python **回溯+巧妙去重(省去使用used** -```python3 +```python class Solution: def __init__(self): self.paths = [] @@ -374,7 +374,7 @@ class Solution: sum_ -= candidates[i] # 回溯,为了下一轮for loop ``` **回溯+去重(使用used)** -```python3 +```python class Solution: def __init__(self): self.paths = [] diff --git a/problems/0042.接雨水.md b/problems/0042.接雨水.md index 27745607..55f80a54 100644 --- a/problems/0042.接雨水.md +++ b/problems/0042.接雨水.md @@ -491,7 +491,7 @@ class Solution: return res ``` 动态规划 -```python3 +```python class Solution: def trap(self, height: List[int]) -> int: leftheight, rightheight = [0]*len(height), [0]*len(height) diff --git a/problems/0046.全排列.md b/problems/0046.全排列.md index 3d31db62..c5369ddd 100644 --- a/problems/0046.全排列.md +++ b/problems/0046.全排列.md @@ -243,7 +243,7 @@ class Solution: usage_list[i] = False ``` **回溯+丢掉usage_list** -```python3 +```python class Solution: def __init__(self): self.path = [] diff --git a/problems/0059.螺旋矩阵II.md b/problems/0059.螺旋矩阵II.md index 3f7a59ca..670d6fc2 100644 --- a/problems/0059.螺旋矩阵II.md +++ b/problems/0059.螺旋矩阵II.md @@ -188,9 +188,9 @@ class Solution { } ``` -python: +python3: -```python3 +```python class Solution: def generateMatrix(self, n: int) -> List[List[int]]: diff --git a/problems/0070.爬楼梯完全背包版本.md b/problems/0070.爬楼梯完全背包版本.md index b5fbb96a..2286de2d 100644 --- a/problems/0070.爬楼梯完全背包版本.md +++ b/problems/0070.爬楼梯完全背包版本.md @@ -143,10 +143,10 @@ class Solution { } ``` -Python: +Python3: -```python3 +```python class Solution: def climbStairs(self, n: int) -> int: dp = [0]*(n + 1) diff --git a/problems/0077.组合优化.md b/problems/0077.组合优化.md index e995fd18..81b4304c 100644 --- a/problems/0077.组合优化.md +++ b/problems/0077.组合优化.md @@ -174,7 +174,7 @@ class Solution { ``` Python: -```python3 +```python class Solution: def combine(self, n: int, k: int) -> List[List[int]]: res=[] #存放符合条件结果的集合 diff --git a/problems/0078.子集.md b/problems/0078.子集.md index 1abf8c95..cdb5f548 100644 --- a/problems/0078.子集.md +++ b/problems/0078.子集.md @@ -203,7 +203,7 @@ class Solution { ``` ## Python -```python3 +```python class Solution: def __init__(self): self.path: List[int] = [] diff --git a/problems/0084.柱状图中最大的矩形.md b/problems/0084.柱状图中最大的矩形.md index 3cb51f1d..439a3bc5 100644 --- a/problems/0084.柱状图中最大的矩形.md +++ b/problems/0084.柱状图中最大的矩形.md @@ -277,9 +277,9 @@ class Solution { } ``` -Python: +Python3: -```python3 +```python # 双指针;暴力解法(leetcode超时) class Solution: diff --git a/problems/0093.复原IP地址.md b/problems/0093.复原IP地址.md index 3e7cd1ad..1fa72cc9 100644 --- a/problems/0093.复原IP地址.md +++ b/problems/0093.复原IP地址.md @@ -339,7 +339,7 @@ class Solution(object): ``` python3: -```python3 +```python class Solution: def __init__(self): self.result = [] diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 1a92d42f..74485848 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -83,10 +83,10 @@ public: }; ``` -python代码: +python3代码: -```python3 +```python class Solution: """二叉树层序遍历迭代解法""" diff --git a/problems/0108.将有序数组转换为二叉搜索树.md b/problems/0108.将有序数组转换为二叉搜索树.md index bd48ea0c..9e008e86 100644 --- a/problems/0108.将有序数组转换为二叉搜索树.md +++ b/problems/0108.将有序数组转换为二叉搜索树.md @@ -305,7 +305,7 @@ class Solution { ## Python **递归** -```python3 +```python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): diff --git a/problems/0110.平衡二叉树.md b/problems/0110.平衡二叉树.md index 90cb7336..d9dcf289 100644 --- a/problems/0110.平衡二叉树.md +++ b/problems/0110.平衡二叉树.md @@ -497,7 +497,7 @@ class Solution { ## Python 递归法: -```python3 +```python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): diff --git a/problems/0129.求根到叶子节点数字之和.md b/problems/0129.求根到叶子节点数字之和.md index 980779c2..b271ca7d 100644 --- a/problems/0129.求根到叶子节点数字之和.md +++ b/problems/0129.求根到叶子节点数字之和.md @@ -217,7 +217,7 @@ class Solution { ``` Python: -```python3 +```python class Solution: def sumNumbers(self, root: TreeNode) -> int: res = 0 diff --git a/problems/0131.分割回文串.md b/problems/0131.分割回文串.md index 439ad8ea..f50f1c1d 100644 --- a/problems/0131.分割回文串.md +++ b/problems/0131.分割回文串.md @@ -289,7 +289,7 @@ class Solution { ## Python **回溯+正反序判断回文串** -```python3 +```python class Solution: def __init__(self): self.paths = [] @@ -326,7 +326,7 @@ class Solution: continue ``` **回溯+函数判断回文串** -```python3 +```python class Solution: def __init__(self): self.paths = [] diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md index 7db75f06..61c558a1 100644 --- a/problems/0188.买卖股票的最佳时机IV.md +++ b/problems/0188.买卖股票的最佳时机IV.md @@ -271,7 +271,7 @@ class Solution: return dp[-1][2*k] ``` 版本二 -```python3 +```python class Solution: def maxProfit(self, k: int, prices: List[int]) -> int: if len(prices) == 0: return 0 diff --git a/problems/0257.二叉树的所有路径.md b/problems/0257.二叉树的所有路径.md index 4078320f..a0c718f4 100644 --- a/problems/0257.二叉树的所有路径.md +++ b/problems/0257.二叉树的所有路径.md @@ -436,7 +436,7 @@ class Solution: 迭代法: -```python3 +```python from collections import deque diff --git a/problems/0279.完全平方数.md b/problems/0279.完全平方数.md index 7bc0c2f7..9bad2085 100644 --- a/problems/0279.完全平方数.md +++ b/problems/0279.完全平方数.md @@ -207,7 +207,7 @@ class Solution { Python: -```python3 +```python class Solution: def numSquares(self, n: int) -> int: '''版本一,先遍历背包, 再遍历物品''' diff --git a/problems/0322.零钱兑换.md b/problems/0322.零钱兑换.md index 8f3438af..3a8d0662 100644 --- a/problems/0322.零钱兑换.md +++ b/problems/0322.零钱兑换.md @@ -207,7 +207,7 @@ class Solution { Python: -```python3 +```python class Solution: def coinChange(self, coins: List[int], amount: int) -> int: '''版本一''' diff --git a/problems/0337.打家劫舍III.md b/problems/0337.打家劫舍III.md index 06831cb6..ecd31d1b 100644 --- a/problems/0337.打家劫舍III.md +++ b/problems/0337.打家劫舍III.md @@ -288,7 +288,7 @@ Python: > 暴力递归 -```python3 +```python # Definition for a binary tree node. # class TreeNode: @@ -315,7 +315,7 @@ class Solution: > 记忆化递归 -```python3 +```python # Definition for a binary tree node. # class TreeNode: @@ -345,7 +345,7 @@ class Solution: ``` > 动态规划 -```python3 +```python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index d75311eb..5076c9ad 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -228,7 +228,7 @@ class Solution { ### Python -```python3 +```python class Solution: def wiggleMaxLength(self, nums: List[int]) -> int: preC,curC,res = 0,0,1 #题目里nums长度大于等于1,当长度为1时,其实到不了for循环里去,所以不用考虑nums长度 diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index 31e19b10..00707347 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -209,7 +209,7 @@ class Solution(object): Python写法四: -```python3 +```python class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: c1 = collections.Counter(ransomNote) diff --git a/problems/0404.左叶子之和.md b/problems/0404.左叶子之和.md index 09272052..8c6eaddb 100644 --- a/problems/0404.左叶子之和.md +++ b/problems/0404.左叶子之和.md @@ -229,7 +229,7 @@ class Solution { ## Python **递归后序遍历** -```python3 +```python class Solution: def sumOfLeftLeaves(self, root: TreeNode) -> int: if not root: @@ -246,7 +246,7 @@ class Solution: ``` **迭代** -```python3 +```python class Solution: def sumOfLeftLeaves(self, root: TreeNode) -> int: """ diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index 1711f638..210b492d 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -146,7 +146,7 @@ class Solution { ``` ### Python -```python3 +```python class Solution: # 思路1:优先考虑胃饼干 def findContentChildren(self, g: List[int], s: List[int]) -> int: diff --git a/problems/0463.岛屿的周长.md b/problems/0463.岛屿的周长.md index e19e06ee..9911dfe5 100644 --- a/problems/0463.岛屿的周长.md +++ b/problems/0463.岛屿的周长.md @@ -124,7 +124,7 @@ Python: ### 解法1: 扫描每个cell,如果当前位置为岛屿 grid[i][j] == 1, 从当前位置判断四边方向,如果边界或者是水域,证明有边界存在,res矩阵的对应cell加一。 -```python3 +```python class Solution: def islandPerimeter(self, grid: List[List[int]]) -> int: diff --git a/problems/0474.一和零.md b/problems/0474.一和零.md index 67e366f4..964df4a8 100644 --- a/problems/0474.一和零.md +++ b/problems/0474.一和零.md @@ -193,7 +193,7 @@ class Solution { ``` Python: -```python3 +```python class Solution: def findMaxForm(self, strs: List[str], m: int, n: int) -> int: dp = [[0] * (n + 1) for _ in range(m + 1)] # 默认初始化0 diff --git a/problems/0491.递增子序列.md b/problems/0491.递增子序列.md index 6d88119e..70b08d50 100644 --- a/problems/0491.递增子序列.md +++ b/problems/0491.递增子序列.md @@ -233,7 +233,7 @@ class Solution { python3 **回溯** -```python3 +```python class Solution: def __init__(self): self.paths = [] @@ -270,7 +270,7 @@ class Solution: self.path.pop() ``` **回溯+哈希表去重** -```python3 +```python class Solution: def __init__(self): self.paths = [] diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md index f190b734..47d0784e 100644 --- a/problems/0494.目标和.md +++ b/problems/0494.目标和.md @@ -160,11 +160,16 @@ dp[j] 表示:填满j(包括j)这么大容积的包,有dp[j]种方法 那么只要搞到nums[i]的话,凑成dp[j]就有dp[j - nums[i]] 种方法。 -举一个例子,nums[i] = 2: dp[3],填满背包容量为3的话,有dp[3]种方法。 -那么只需要搞到一个2(nums[i]),有dp[3]方法可以凑齐容量为3的背包,相应的就有多少种方法可以凑齐容量为5的背包。 +例如:dp[j],j 为5, -那么需要把 这些方法累加起来就可以了,dp[j] += dp[j - nums[i]] +* 已经有一个1(nums[i]) 的话,有 dp[4]种方法 凑成 dp[5]。 +* 已经有一个2(nums[i]) 的话,有 dp[3]种方法 凑成 dp[5]。 +* 已经有一个3(nums[i]) 的话,有 dp[2]中方法 凑成 dp[5] +* 已经有一个4(nums[i]) 的话,有 dp[1]中方法 凑成 dp[5] +* 已经有一个5 (nums[i])的话,有 dp[0]中方法 凑成 dp[5] + +那么凑整dp[5]有多少方法呢,也就是把 所有的 dp[j - nums[i]] 累加起来。 所以求组合类问题的公式,都是类似这种: diff --git a/problems/0496.下一个更大元素I.md b/problems/0496.下一个更大元素I.md index f039c198..f9dfa308 100644 --- a/problems/0496.下一个更大元素I.md +++ b/problems/0496.下一个更大元素I.md @@ -222,8 +222,8 @@ class Solution { } } ``` -Python: -```python3 +Python3: +```python class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: result = [-1]*len(nums1) diff --git a/problems/0501.二叉搜索树中的众数.md b/problems/0501.二叉搜索树中的众数.md index 277f46f5..a2984ecc 100644 --- a/problems/0501.二叉搜索树中的众数.md +++ b/problems/0501.二叉搜索树中的众数.md @@ -470,7 +470,7 @@ class Solution { > 递归法 -```python3 +```python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): @@ -515,7 +515,7 @@ class Solution: > 迭代法-中序遍历-不使用额外空间,利用二叉搜索树特性 -```python3 +```python class Solution: def findMode(self, root: TreeNode) -> List[int]: stack = [] diff --git a/problems/0503.下一个更大元素II.md b/problems/0503.下一个更大元素II.md index c9532a22..36e183e1 100644 --- a/problems/0503.下一个更大元素II.md +++ b/problems/0503.下一个更大元素II.md @@ -124,7 +124,7 @@ class Solution { ``` Python: -```python3 +```python class Solution: def nextGreaterElements(self, nums: List[int]) -> List[int]: dp = [-1] * len(nums) diff --git a/problems/0518.零钱兑换II.md b/problems/0518.零钱兑换II.md index be60ac13..e72c5f85 100644 --- a/problems/0518.零钱兑换II.md +++ b/problems/0518.零钱兑换II.md @@ -207,7 +207,7 @@ class Solution { Python: -```python3 +```python class Solution: def change(self, amount: int, coins: List[int]) -> int: dp = [0]*(amount + 1) diff --git a/problems/0538.把二叉搜索树转换为累加树.md b/problems/0538.把二叉搜索树转换为累加树.md index 1d11d4ee..1b07b803 100644 --- a/problems/0538.把二叉搜索树转换为累加树.md +++ b/problems/0538.把二叉搜索树转换为累加树.md @@ -196,7 +196,7 @@ class Solution { ## Python **递归** -```python3 +```python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): diff --git a/problems/0669.修剪二叉搜索树.md b/problems/0669.修剪二叉搜索树.md index d17c0b1c..15f6a040 100644 --- a/problems/0669.修剪二叉搜索树.md +++ b/problems/0669.修剪二叉搜索树.md @@ -264,7 +264,7 @@ class Solution { ## Python **递归** -```python3 +```python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): diff --git a/problems/0841.钥匙和房间.md b/problems/0841.钥匙和房间.md index 8397690e..1cd13065 100644 --- a/problems/0841.钥匙和房间.md +++ b/problems/0841.钥匙和房间.md @@ -152,7 +152,6 @@ class Solution { -Python: python3 diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index 9a0bb1c1..b94e557d 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -194,7 +194,7 @@ class Solution { ``` Python: -```python3 +```python # 方法一,使用栈,推荐! class Solution: def removeDuplicates(self, s: str) -> str: @@ -207,7 +207,7 @@ class Solution: return "".join(res) # 字符串拼接 ``` -```python3 +```python # 方法二,使用双指针模拟栈,如果不让用栈可以作为备选方法。 class Solution: def removeDuplicates(self, s: str) -> str: diff --git a/problems/二叉树的递归遍历.md b/problems/二叉树的递归遍历.md index c481fd11..2fef68da 100644 --- a/problems/二叉树的递归遍历.md +++ b/problems/二叉树的递归遍历.md @@ -168,7 +168,7 @@ class Solution { ``` Python: -```python3 +```python # 前序遍历-递归-LC144_二叉树的前序遍历 class Solution: def preorderTraversal(self, root: TreeNode) -> List[int]: diff --git a/problems/周总结/20201003二叉树周末总结.md b/problems/周总结/20201003二叉树周末总结.md index a0b8c2dd..18bbf37f 100644 --- a/problems/周总结/20201003二叉树周末总结.md +++ b/problems/周总结/20201003二叉树周末总结.md @@ -34,8 +34,8 @@ public: // 此时就是:左右节点都不为空,且数值相同的情况 // 此时才做递归,做下一层的判断 - bool outside = compare(left->left, right->right); // 左子树:左、 右子树:左 (相对于求对称二叉树,只需改一下这里的顺序) - bool inside = compare(left->right, right->left); // 左子树:右、 右子树:右 + bool outside = compare(left->left, right->left); // 左子树:左、 右子树:左 (相对于求对称二叉树,只需改一下这里的顺序) + bool inside = compare(left->right, right->right); // 左子树:右、 右子树:右 bool isSame = outside && inside; // 左子树:中、 右子树:中 (逻辑处理) return isSame; diff --git a/problems/背包问题理论基础完全背包.md b/problems/背包问题理论基础完全背包.md index cea69c72..7abaf160 100644 --- a/problems/背包问题理论基础完全背包.md +++ b/problems/背包问题理论基础完全背包.md @@ -216,7 +216,7 @@ private static void testCompletePackAnotherWay(){ Python: -```python3 +```python # 先遍历物品,再遍历背包 def test_complete_pack1(): weight = [1, 3, 4]