Skip to content

Commit cc341f3

Browse files
authored
feat: add go solution to lc problem: No.0444 (doocs#868)
1 parent 2767cef commit cc341f3

File tree

5 files changed

+77
-1
lines changed

5 files changed

+77
-1
lines changed

solution/0400-0499/0402.Remove K Digits/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
- 但可以选择性丢弃前面的相邻元素,丢弃与否取决于当前元素和前面相邻元素的大小;
5959
- 根据前置知识可知当当前元素小于前面相邻元素时可以移除前面相邻的元素。
6060

61+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。
62+
6163
<!-- tabs:start -->
6264

6365
### **Python3**

solution/0400-0499/0403.Frog Jump/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@
4848
动态规划转移方程如下:
4949

5050
$$
51-
dp[i][k] = dp[j][k-1] || dp[j][k] || dp[j][k+1]
51+
dp[i][k] = dp[j][k-1] \ || \ dp[j][k] \ || \ dp[j][k+1]
5252
$$
5353

5454
其中 `dp[i][k]` 表示最后一次跳跃为 `k` 个单位时,能否到达 `i`,定义 base case 为 `dp[0][0] = True`(起点在下标 0)。
5555

5656
对于从 `j` 跳到 `i` 的青蛙,因为跳跃的距离确定为 `k` 个单位,所以根据题意最后一次跳跃到达 `j` 的跳跃距离只能选择为 `k - 1``k``k + 1` 个单位,故只要 `dp[j][k - 1], dp[j][k], dp[j][k + 1]` 中有任一为 `True`,即可从 `j` 跳跃到 `i`
5757

58+
时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。
59+
5860
**方法二:回溯+剪枝**
5961

6062
这是最直观的解题思路。显然青蛙在第 `1` 个石子的起始跳跃距离为 `1`,对于第 `2` 个石子,根据题意很容易得到青蛙的跳跃距离只能是 `0、1 或 2`。依次类推,可以得到青蛙在第 `i` 个石子可能的跳跃距离集合,借助这个思路验证当青蛙在 `i` 处跳跃距离为集合之一时是否可以刚好过河,如不能过河继续验证其他取值即可。

solution/0400-0499/0404.Sum of Left Leaves/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,33 @@ impl Solution {
189189
}
190190
```
191191

192+
### **Go**
193+
194+
<!-- 这里可写当前语言的特殊实现逻辑 -->
195+
196+
```go
197+
/**
198+
* Definition for a binary tree node.
199+
* type TreeNode struct {
200+
* Val int
201+
* Left *TreeNode
202+
* Right *TreeNode
203+
* }
204+
*/
205+
func sumOfLeftLeaves(root *TreeNode) int {
206+
if root == nil {
207+
return 0
208+
}
209+
res := 0
210+
if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil {
211+
res += root.Left.Val
212+
}
213+
res += sumOfLeftLeaves(root.Left)
214+
res += sumOfLeftLeaves(root.Right)
215+
return res
216+
}
217+
```
218+
192219
### **...**
193220

194221
```

solution/0400-0499/0404.Sum of Left Leaves/README_EN.md

+25
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,31 @@ impl Solution {
177177
}
178178
```
179179

180+
### **Go**
181+
182+
```go
183+
/**
184+
* Definition for a binary tree node.
185+
* type TreeNode struct {
186+
* Val int
187+
* Left *TreeNode
188+
* Right *TreeNode
189+
* }
190+
*/
191+
func sumOfLeftLeaves(root *TreeNode) int {
192+
if root == nil {
193+
return 0
194+
}
195+
res := 0
196+
if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil {
197+
res += root.Left.Val
198+
}
199+
res += sumOfLeftLeaves(root.Left)
200+
res += sumOfLeftLeaves(root.Right)
201+
return res
202+
}
203+
```
204+
180205
### **...**
181206

182207
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func sumOfLeftLeaves(root *TreeNode) int {
10+
if root == nil {
11+
return 0
12+
}
13+
res := 0
14+
if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil {
15+
res += root.Left.Val
16+
}
17+
res += sumOfLeftLeaves(root.Left)
18+
res += sumOfLeftLeaves(root.Right)
19+
return res
20+
}

0 commit comments

Comments
 (0)