Skip to content

Commit 10a006b

Browse files
committed
Merge branch 'youngyangyang04:master' into master
2 parents fdb48d9 + a4b7399 commit 10a006b

8 files changed

+261
-2
lines changed

problems/0198.打家劫舍.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,20 @@ class Solution {
131131
```
132132

133133
Python:
134-
134+
```python
135+
class Solution:
136+
def rob(self, nums: List[int]) -> int:
137+
if len(nums) == 0:
138+
return 0
139+
if len(nums) == 1:
140+
return nums[0]
141+
dp = [0] * len(nums)
142+
dp[0] = nums[0]
143+
dp[1] = max(nums[0], nums[1])
144+
for i in range(2, len(nums)):
145+
dp[i] = max(dp[i-2]+nums[i], dp[i-1])
146+
return dp[-1]
147+
```
135148

136149
Go:
137150
```Go

problems/0222.完全二叉树的节点个数.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,35 @@ class Solution:
308308

309309
Go:
310310

311+
递归版本
312+
313+
```go
314+
/**
315+
* Definition for a binary tree node.
316+
* type TreeNode struct {
317+
* Val int
318+
* Left *TreeNode
319+
* Right *TreeNode
320+
* }
321+
*/
322+
//本题直接就是求有多少个节点,无脑存进数组算长度就行了。
323+
func countNodes(root *TreeNode) int {
324+
if root == nil {
325+
return 0
326+
}
327+
res := 1
328+
if root.Right != nil {
329+
res += countNodes(root.Right)
330+
}
331+
if root.Left != nil {
332+
res += countNodes(root.Left)
333+
}
334+
return res
335+
}
336+
```
337+
338+
339+
311340
JavaScript:
312341

313342
递归版本

problems/0337.打家劫舍III.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,25 @@ class Solution {
287287

288288
Python:
289289

290+
> 动态规划
291+
```python
292+
class Solution:
293+
def rob(self, root: TreeNode) -> int:
294+
result = self.robTree(root)
295+
return max(result[0], result[1])
296+
297+
#长度为2的数组,0:不偷,1:偷
298+
def robTree(self, cur):
299+
if not cur:
300+
return (0, 0) #这里返回tuple, 也可以返回list
301+
left = self.robTree(cur.left)
302+
right = self.robTree(cur.right)
303+
#偷cur
304+
val1 = cur.val + left[0] + right[0]
305+
#不偷cur
306+
val2 = max(left[0], left[1]) + max(right[0], right[1])
307+
return (val2, val1)
308+
```
290309

291310
Go:
292311

problems/0474.一和零.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,43 @@ class Solution:
206206
```
207207

208208
Go:
209+
```go
210+
func findMaxForm(strs []string, m int, n int) int {
211+
// 定义数组
212+
dp := make([][]int, m+1)
213+
for i,_ := range dp {
214+
dp[i] = make([]int, n+1 )
215+
}
216+
// 遍历
217+
for i:=0;i<len(strs);i++ {
218+
zeroNum,oneNum := 0 , 0
219+
//计算0,1 个数
220+
//或者直接strings.Count(strs[i],"0")
221+
for _,v := range strs[i] {
222+
if v == '0' {
223+
zeroNum++
224+
}
225+
}
226+
oneNum = len(strs[i])-zeroNum
227+
// 从后往前 遍历背包容量
228+
for j:= m ; j >= zeroNum;j-- {
229+
for k:=n ; k >= oneNum;k-- {
230+
// 推导公式
231+
dp[j][k] = max(dp[j][k],dp[j-zeroNum][k-oneNum]+1)
232+
}
233+
}
234+
//fmt.Println(dp)
235+
}
236+
return dp[m][n]
237+
}
209238

239+
func max(a,b int) int {
240+
if a > b {
241+
return a
242+
}
243+
return b
244+
}
245+
```
210246

211247

212248

problems/0530.二叉搜索树的最小绝对差.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,30 @@ public:
151151
152152
153153
Java:
154-
154+
递归
155+
```java
156+
class Solution {
157+
TreeNode pre;// 记录上一个遍历的结点
158+
int result = Integer.MAX_VALUE;
159+
public int getMinimumDifference(TreeNode root) {
160+
if(root==null)return 0;
161+
traversal(root);
162+
return result;
163+
}
164+
public void traversal(TreeNode root){
165+
if(root==null)return;
166+
//左
167+
traversal(root.left);
168+
//中
169+
if(pre!=null){
170+
result = Math.min(result,root.val-pre.val);
171+
}
172+
pre = root;
173+
//右
174+
traversal(root.right);
175+
}
176+
}
177+
```
155178
```Java
156179
class Solution {
157180
TreeNode pre;// 记录上一个遍历的结点

problems/二叉树中递归带着回溯.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,83 @@ Java:
257257

258258
Python:
259259

260+
100.相同的树
261+
> 递归法
262+
```python
263+
class Solution:
264+
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
265+
return self.compare(p, q)
266+
267+
def compare(self, tree1, tree2):
268+
if not tree1 and tree2:
269+
return False
270+
elif tree1 and not tree2:
271+
return False
272+
elif not tree1 and not tree2:
273+
return True
274+
elif tree1.val != tree2.val: #注意这里我没有使用else
275+
return False
276+
277+
#此时就是:左右节点都不为空,且数值相同的情况
278+
#此时才做递归,做下一层的判断
279+
compareLeft = self.compare(tree1.left, tree2.left) #左子树:左、 右子树:左
280+
compareRight = self.compare(tree1.right, tree2.right) #左子树:右、 右子树:右
281+
isSame = compareLeft and compareRight #左子树:中、 右子树:中(逻辑处理)
282+
return isSame
283+
```
284+
285+
257.二叉的所有路径
286+
> 递归中隐藏着回溯
287+
```python
288+
class Solution:
289+
def binaryTreePaths(self, root: TreeNode) -> List[str]:
290+
result = []
291+
path = []
292+
if not root:
293+
return result
294+
self.traversal(root, path, result)
295+
return result
296+
297+
def traversal(self, cur, path, result):
298+
path.append(cur.val)
299+
#这才到了叶子节点
300+
if not cur.left and not cur.right:
301+
sPath = ""
302+
for i in range(len(path)-1):
303+
sPath += str(path[i])
304+
sPath += "->"
305+
sPath += str(path[len(path)-1])
306+
result.append(sPath)
307+
return
308+
if cur.left:
309+
self.traversal(cur.left, path, result)
310+
path.pop() #回溯
311+
if cur.right:
312+
self.traversal(cur.right, path, result)
313+
path.pop() #回溯
314+
```
315+
316+
> 精简版
317+
```python
318+
class Solution:
319+
def binaryTreePaths(self, root: TreeNode) -> List[str]:
320+
result = []
321+
path = ""
322+
if not root:
323+
return result
324+
self.traversal(root, path, result)
325+
return result
326+
327+
def traversal(self, cur, path, result):
328+
path += str(cur.val) #
329+
if not cur.left and not cur.right:
330+
result.append(path)
331+
return
332+
if cur.left:
333+
self.traversal(cur.left, path+"->", result) #左 回溯就隐藏在这里
334+
if cur.right:
335+
self.traversal(cur.right, path+"->", result) #右 回溯就隐藏在这里
336+
```
260337

261338
Go:
262339

problems/背包理论基础01背包-1.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,40 @@ Python:
273273

274274

275275
Go:
276+
```go
277+
func test_2_wei_bag_problem1(weight, value []int, bagWeight int) int {
278+
// 定义dp数组
279+
dp := make([][]int, len(weight))
280+
for i, _ := range dp {
281+
dp[i] = make([]int, bagWeight+1)
282+
}
283+
// 初始化
284+
for j := bagWeight; j >= weight[0]; j-- {
285+
dp[0][j] = dp[0][j-weight[0]] + value[0]
286+
}
287+
// 递推公式
288+
for i := 1; i < len(weight); i++ {
289+
//正序,也可以倒序
290+
for j := weight[i];j<= bagWeight ; j++ {
291+
dp[i][j] = max(dp[i-1][j], dp[i-1][j-weight[i]]+value[i])
292+
}
293+
}
294+
return dp[len(weight)-1][bagWeight]
295+
}
296+
297+
func max(a,b int) int {
298+
if a > b {
299+
return a
300+
}
301+
return b
302+
}
276303

304+
func main() {
305+
weight := []int{1,3,4}
306+
value := []int{15,20,30}
307+
test_2_wei_bag_problem1(weight,value,4)
308+
}
309+
```
277310

278311
-----------------------
279312
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

problems/背包理论基础01背包-2.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,37 @@ Python:
219219

220220

221221
Go:
222+
```go
223+
func test_1_wei_bag_problem(weight, value []int, bagWeight int) int {
224+
// 定义 and 初始化
225+
dp := make([]int,bagWeight+1)
226+
// 递推顺序
227+
for i := 0 ;i < len(weight) ; i++ {
228+
// 这里必须倒序,区别二维,因为二维dp保存了i的状态
229+
for j:= bagWeight; j >= weight[i] ; j-- {
230+
// 递推公式
231+
dp[j] = max(dp[j], dp[j-weight[i]]+value[i])
232+
}
233+
}
234+
//fmt.Println(dp)
235+
return dp[bagWeight]
236+
}
237+
238+
func max(a,b int) int {
239+
if a > b {
240+
return a
241+
}
242+
return b
243+
}
222244

223245

246+
func main() {
247+
weight := []int{1,3,4}
248+
value := []int{15,20,30}
249+
test_1_wei_bag_problem(weight,value,4)
250+
}
251+
```
252+
224253

225254

226255
-----------------------

0 commit comments

Comments
 (0)