Skip to content

Commit 854a339

Browse files
authored
Merge pull request #34 from halfrost/add_hugo
Add hugo
2 parents 688a384 + beccf9f commit 854a339

File tree

604 files changed

+51508
-1999
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

604 files changed

+51508
-1999
lines changed

.github/workflows/deploy.yml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
push:
55
branches:
66
- master # 只在master上push触发部署
7+
- add_hugo
78
paths-ignore: # 下列文件的变更不触发部署,可以自行添加
89
- README.md
910
- LICENSE
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package leetcode
2+
3+
func canJump(nums []int) bool {
4+
n := len(nums)
5+
if n == 0 {
6+
return false
7+
}
8+
if n == 1 {
9+
return true
10+
}
11+
maxJump := 0
12+
for i, v := range nums {
13+
if i > maxJump {
14+
return false
15+
}
16+
maxJump = max(maxJump, i+v)
17+
}
18+
return true
19+
}
20+
21+
func max(a int, b int) int {
22+
if a > b {
23+
return a
24+
}
25+
return b
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question55 struct {
9+
para55
10+
ans55
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para55 struct {
16+
one []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans55 struct {
22+
one bool
23+
}
24+
25+
func Test_Problem55(t *testing.T) {
26+
27+
qs := []question55{
28+
29+
question55{
30+
para55{[]int{2, 3, 1, 1, 4}},
31+
ans55{true},
32+
},
33+
question55{
34+
para55{[]int{3, 2, 1, 0, 4}},
35+
ans55{false},
36+
},
37+
}
38+
39+
fmt.Printf("------------------------Leetcode Problem 55------------------------\n")
40+
41+
for _, q := range qs {
42+
_, p := q.ans55, q.para55
43+
fmt.Printf("【input】:%v 【output】:%v\n", p, canJump(p.one))
44+
}
45+
fmt.Printf("\n\n\n")
46+
}

Algorithms/0055.Jump-Game/README.md

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# 55. Jump Game
2+
3+
4+
## 题目
5+
6+
Given an array of non-negative integers, you are initially positioned at the first index of the array.
7+
8+
Each element in the array represents your maximum jump length at that position.
9+
10+
Determine if you are able to reach the last index.
11+
12+
**Example 1**:
13+
14+
```
15+
Input: [2,3,1,1,4]
16+
Output: true
17+
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
18+
```
19+
20+
**Example 2**:
21+
22+
```
23+
Input: [3,2,1,0,4]
24+
Output: false
25+
Explanation: You will always arrive at index 3 no matter what. Its maximum
26+
jump length is 0, which makes it impossible to reach the last index.
27+
```
28+
29+
## 题目大意
30+
31+
给定一个非负整数数组,最初位于数组的第一个位置。数组中的每个元素代表在该位置可以跳跃的最大长度。判断是否能够到达最后一个位置。
32+
33+
## 解题思路
34+
35+
- 给出一个非负数组,要求判断从数组 0 下标开始,能否到达数组最后一个位置。
36+
- 这一题比较简单。如果某一个作为 `起跳点` 的格子可以跳跃的距离是 `n`,那么表示后面 `n` 个格子都可以作为 `起跳点`。可以对每一个能作为 `起跳点` 的格子都尝试跳一次,把 `能跳到最远的距离maxJump` 不断更新。如果可以一直跳到最后,就成功了。如果中间有一个点比 `maxJump` 还要大,说明在这个点和 maxJump 中间连不上了,有些点不能到达最后一个位置。
37+
38+
## 代码
39+
40+
```go
41+
func canJump(nums []int) bool {
42+
n := len(nums)
43+
if n == 0 {
44+
return false
45+
}
46+
if n == 1 {
47+
return true
48+
}
49+
maxJump := 0
50+
for i, v := range nums {
51+
if i > maxJump {
52+
return false
53+
}
54+
maxJump = max(maxJump, i+v)
55+
}
56+
return true
57+
}
58+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package leetcode
2+
3+
import (
4+
"github.com/halfrost/LeetCode-Go/structures"
5+
)
6+
7+
// TreeNode define
8+
type TreeNode = structures.TreeNode
9+
10+
/**
11+
* Definition for a binary tree node.
12+
* type TreeNode struct {
13+
* Val int
14+
* Left *TreeNode
15+
* Right *TreeNode
16+
* }
17+
*/
18+
func rob337(root *TreeNode) int {
19+
a, b := dfsTreeRob(root)
20+
return max(a, b)
21+
}
22+
23+
func dfsTreeRob(root *TreeNode) (a, b int) {
24+
if root == nil {
25+
return 0, 0
26+
}
27+
l0, l1 := dfsTreeRob(root.Left)
28+
r0, r1 := dfsTreeRob(root.Right)
29+
// 当前节点没有被打劫
30+
tmp0 := max(l0, l1) + max(r0, r1)
31+
// 当前节点被打劫
32+
tmp1 := root.Val + l0 + r0
33+
return tmp0, tmp1
34+
}
35+
36+
func max(a int, b int) int {
37+
if a > b {
38+
return a
39+
}
40+
return b
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/halfrost/LeetCode-Go/structures"
8+
)
9+
10+
type question337 struct {
11+
para337
12+
ans337
13+
}
14+
15+
// para 是参数
16+
// one 代表第一个参数
17+
type para337 struct {
18+
one []int
19+
}
20+
21+
// ans 是答案
22+
// one 代表第一个答案
23+
type ans337 struct {
24+
one int
25+
}
26+
27+
func Test_Problem337(t *testing.T) {
28+
29+
qs := []question337{
30+
31+
question337{
32+
para337{[]int{3, 2, 3, structures.NULL, 3, structures.NULL, 1}},
33+
ans337{7},
34+
},
35+
36+
question337{
37+
para337{[]int{}},
38+
ans337{0},
39+
},
40+
41+
question337{
42+
para337{[]int{3, 4, 5, 1, 3, structures.NULL, 1}},
43+
ans337{9},
44+
},
45+
}
46+
47+
fmt.Printf("------------------------Leetcode Problem 337------------------------\n")
48+
49+
for _, q := range qs {
50+
_, p := q.ans337, q.para337
51+
fmt.Printf("【input】:%v 【output】:%v\n", p, rob337(structures.Ints2TreeNode(p.one)))
52+
}
53+
fmt.Printf("\n\n\n")
54+
}
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# 337. House Robber III
2+
3+
4+
## 题目
5+
6+
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night.
7+
8+
Determine the maximum amount of money the thief can rob tonight without alerting the police.
9+
10+
**Example 1**:
11+
12+
```
13+
Input: [3,2,3,null,3,null,1]
14+
15+
3
16+
/ \
17+
2 3
18+
\ \
19+
3 1
20+
21+
Output: 7
22+
Explanation: Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
23+
```
24+
25+
**Example 2**:
26+
27+
```
28+
Input: [3,4,5,1,3,null,1]
29+
30+
3
31+
/ \
32+
4 5
33+
/ \ \
34+
1 3 1
35+
36+
Output: 9
37+
Explanation: Maximum amount of money the thief can rob = 4 + 5 = 9.
38+
```
39+
40+
## 题目大意
41+
42+
一个新的可行窃的地区只有一个入口,称之为“根”。 除了“根”之外,每栋房子有且只有一个“父“房子与之相连。一番侦察之后,聪明的小偷意识到“这个地方的所有房屋的排列类似于一棵二叉树”。 如果两个直接相连的房子在同一天晚上被打劫,房屋将自动报警。计算在不触动警报的情况下,小偷一晚能够盗取的最高金额。
43+
44+
45+
## 解题思路
46+
47+
- 这一题是打家劫舍的第 3 题。这一题需要偷的房子是树状的。报警的条件还是相邻的房子如果都被偷了,就会触发报警。只不过这里相邻的房子是树上的。问小偷在不触发报警的条件下最终能偷的最高金额。
48+
- 解题思路是 DFS。当前节点是否被打劫,会产生 2 种结果。如果当前节点被打劫,那么它的孩子节点可以被打劫;如果当前节点没有被打劫,那么它的孩子节点不能被打劫。按照这个逻辑递归,最终递归到根节点,取最大值输出即可。
49+
50+
## 代码
51+
52+
```go
53+
54+
func rob337(root *TreeNode) int {
55+
a, b := dfsTreeRob(root)
56+
return max(a, b)
57+
}
58+
59+
func dfsTreeRob(root *TreeNode) (a, b int) {
60+
if root == nil {
61+
return 0, 0
62+
}
63+
l0, l1 := dfsTreeRob(root.Left)
64+
r0, r1 := dfsTreeRob(root.Right)
65+
// 当前节点没有被打劫
66+
tmp0 := max(l0, l1) + max(r0, r1)
67+
// 当前节点被打劫
68+
tmp1 := root.Val + l0 + r0
69+
return tmp0, tmp1
70+
}
71+
72+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package leetcode
2+
3+
// 解法一 DP
4+
func findTargetSumWays(nums []int, S int) int {
5+
total := 0
6+
for _, n := range nums {
7+
total += n
8+
}
9+
if S > total || (S+total)%2 == 1 {
10+
return 0
11+
}
12+
target := (S + total) / 2
13+
dp := make([]int, target+1)
14+
dp[0] = 1
15+
for _, n := range nums {
16+
for i := target; i >= n; i-- {
17+
dp[i] += dp[i-n]
18+
}
19+
}
20+
return dp[target]
21+
}
22+
23+
// 解法二 DFS
24+
func findTargetSumWays1(nums []int, S int) int {
25+
// sums[i] 存储的是后缀和 nums[i:],即从 i 到结尾的和
26+
sums := make([]int, len(nums))
27+
sums[len(nums)-1] = nums[len(nums)-1]
28+
for i := len(nums) - 2; i > -1; i-- {
29+
sums[i] = sums[i+1] + nums[i]
30+
}
31+
res := 0
32+
dfsFindTargetSumWays(nums, 0, 0, S, &res, sums)
33+
return res
34+
}
35+
36+
func dfsFindTargetSumWays(nums []int, index int, curSum int, S int, res *int, sums []int) {
37+
if index == len(nums) {
38+
if curSum == S {
39+
*(res) = *(res) + 1
40+
}
41+
return
42+
}
43+
// 剪枝优化:如果 sums[index] 值小于剩下需要正数的值,那么右边就算都是 + 号也无能为力了,所以这里可以剪枝了
44+
if S-curSum > sums[index] {
45+
return
46+
}
47+
dfsFindTargetSumWays(nums, index+1, curSum+nums[index], S, res, sums)
48+
dfsFindTargetSumWays(nums, index+1, curSum-nums[index], S, res, sums)
49+
}

0 commit comments

Comments
 (0)