Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e40ddd5

Browse files
committed
0337.打家劫舍III.md 使用 slices.Max 替代手工max func。
1 parent 5e0ab49 commit e40ddd5

File tree

1 file changed

+2
-9
lines changed

1 file changed

+2
-9
lines changed

problems/0337.打家劫舍III.md

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -477,14 +477,7 @@ func max(x, y int) int {
477477
```go
478478
func rob(root *TreeNode) int {
479479
res := robTree(root)
480-
return max(res[0], res[1])
481-
}
482-
483-
func max(a, b int) int {
484-
if a > b {
485-
return a
486-
}
487-
return b
480+
return slices.Max(res)
488481
}
489482

490483
func robTree(cur *TreeNode) []int {
@@ -498,7 +491,7 @@ func robTree(cur *TreeNode) []int {
498491
// 考虑去偷当前的屋子
499492
robCur := cur.Val + left[0] + right[0]
500493
// 考虑不去偷当前的屋子
501-
notRobCur := max(left[0], left[1]) + max(right[0], right[1])
494+
notRobCur := slices.Max(left) + slices.Max(right)
502495

503496
// 注意顺序:0:不偷,1:去偷
504497
return []int{notRobCur, robCur}

0 commit comments

Comments
 (0)