forked from halfrost/LeetCode-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request halfrost#34 from halfrost/add_hugo
Add hugo
- Loading branch information
Showing
604 changed files
with
51,508 additions
and
1,999 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package leetcode | ||
|
||
func canJump(nums []int) bool { | ||
n := len(nums) | ||
if n == 0 { | ||
return false | ||
} | ||
if n == 1 { | ||
return true | ||
} | ||
maxJump := 0 | ||
for i, v := range nums { | ||
if i > maxJump { | ||
return false | ||
} | ||
maxJump = max(maxJump, i+v) | ||
} | ||
return true | ||
} | ||
|
||
func max(a int, b int) int { | ||
if a > b { | ||
return a | ||
} | ||
return b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package leetcode | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
type question55 struct { | ||
para55 | ||
ans55 | ||
} | ||
|
||
// para 是参数 | ||
// one 代表第一个参数 | ||
type para55 struct { | ||
one []int | ||
} | ||
|
||
// ans 是答案 | ||
// one 代表第一个答案 | ||
type ans55 struct { | ||
one bool | ||
} | ||
|
||
func Test_Problem55(t *testing.T) { | ||
|
||
qs := []question55{ | ||
|
||
question55{ | ||
para55{[]int{2, 3, 1, 1, 4}}, | ||
ans55{true}, | ||
}, | ||
question55{ | ||
para55{[]int{3, 2, 1, 0, 4}}, | ||
ans55{false}, | ||
}, | ||
} | ||
|
||
fmt.Printf("------------------------Leetcode Problem 55------------------------\n") | ||
|
||
for _, q := range qs { | ||
_, p := q.ans55, q.para55 | ||
fmt.Printf("【input】:%v 【output】:%v\n", p, canJump(p.one)) | ||
} | ||
fmt.Printf("\n\n\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# 55. Jump Game | ||
|
||
|
||
## 题目 | ||
|
||
Given an array of non-negative integers, you are initially positioned at the first index of the array. | ||
|
||
Each element in the array represents your maximum jump length at that position. | ||
|
||
Determine if you are able to reach the last index. | ||
|
||
**Example 1**: | ||
|
||
``` | ||
Input: [2,3,1,1,4] | ||
Output: true | ||
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. | ||
``` | ||
|
||
**Example 2**: | ||
|
||
``` | ||
Input: [3,2,1,0,4] | ||
Output: false | ||
Explanation: You will always arrive at index 3 no matter what. Its maximum | ||
jump length is 0, which makes it impossible to reach the last index. | ||
``` | ||
|
||
## 题目大意 | ||
|
||
给定一个非负整数数组,最初位于数组的第一个位置。数组中的每个元素代表在该位置可以跳跃的最大长度。判断是否能够到达最后一个位置。 | ||
|
||
## 解题思路 | ||
|
||
- 给出一个非负数组,要求判断从数组 0 下标开始,能否到达数组最后一个位置。 | ||
- 这一题比较简单。如果某一个作为 `起跳点` 的格子可以跳跃的距离是 `n`,那么表示后面 `n` 个格子都可以作为 `起跳点`。可以对每一个能作为 `起跳点` 的格子都尝试跳一次,把 `能跳到最远的距离maxJump` 不断更新。如果可以一直跳到最后,就成功了。如果中间有一个点比 `maxJump` 还要大,说明在这个点和 maxJump 中间连不上了,有些点不能到达最后一个位置。 | ||
|
||
## 代码 | ||
|
||
```go | ||
func canJump(nums []int) bool { | ||
n := len(nums) | ||
if n == 0 { | ||
return false | ||
} | ||
if n == 1 { | ||
return true | ||
} | ||
maxJump := 0 | ||
for i, v := range nums { | ||
if i > maxJump { | ||
return false | ||
} | ||
maxJump = max(maxJump, i+v) | ||
} | ||
return true | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package leetcode | ||
|
||
import ( | ||
"github.com/halfrost/LeetCode-Go/structures" | ||
) | ||
|
||
// TreeNode define | ||
type TreeNode = structures.TreeNode | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* type TreeNode struct { | ||
* Val int | ||
* Left *TreeNode | ||
* Right *TreeNode | ||
* } | ||
*/ | ||
func rob337(root *TreeNode) int { | ||
a, b := dfsTreeRob(root) | ||
return max(a, b) | ||
} | ||
|
||
func dfsTreeRob(root *TreeNode) (a, b int) { | ||
if root == nil { | ||
return 0, 0 | ||
} | ||
l0, l1 := dfsTreeRob(root.Left) | ||
r0, r1 := dfsTreeRob(root.Right) | ||
// 当前节点没有被打劫 | ||
tmp0 := max(l0, l1) + max(r0, r1) | ||
// 当前节点被打劫 | ||
tmp1 := root.Val + l0 + r0 | ||
return tmp0, tmp1 | ||
} | ||
|
||
func max(a int, b int) int { | ||
if a > b { | ||
return a | ||
} | ||
return b | ||
} |
54 changes: 54 additions & 0 deletions
54
Algorithms/0337.House-Robber-III/337. House Robber III_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package leetcode | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/halfrost/LeetCode-Go/structures" | ||
) | ||
|
||
type question337 struct { | ||
para337 | ||
ans337 | ||
} | ||
|
||
// para 是参数 | ||
// one 代表第一个参数 | ||
type para337 struct { | ||
one []int | ||
} | ||
|
||
// ans 是答案 | ||
// one 代表第一个答案 | ||
type ans337 struct { | ||
one int | ||
} | ||
|
||
func Test_Problem337(t *testing.T) { | ||
|
||
qs := []question337{ | ||
|
||
question337{ | ||
para337{[]int{3, 2, 3, structures.NULL, 3, structures.NULL, 1}}, | ||
ans337{7}, | ||
}, | ||
|
||
question337{ | ||
para337{[]int{}}, | ||
ans337{0}, | ||
}, | ||
|
||
question337{ | ||
para337{[]int{3, 4, 5, 1, 3, structures.NULL, 1}}, | ||
ans337{9}, | ||
}, | ||
} | ||
|
||
fmt.Printf("------------------------Leetcode Problem 337------------------------\n") | ||
|
||
for _, q := range qs { | ||
_, p := q.ans337, q.para337 | ||
fmt.Printf("【input】:%v 【output】:%v\n", p, rob337(structures.Ints2TreeNode(p.one))) | ||
} | ||
fmt.Printf("\n\n\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# 337. House Robber III | ||
|
||
|
||
## 题目 | ||
|
||
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. | ||
|
||
Determine the maximum amount of money the thief can rob tonight without alerting the police. | ||
|
||
**Example 1**: | ||
|
||
``` | ||
Input: [3,2,3,null,3,null,1] | ||
3 | ||
/ \ | ||
2 3 | ||
\ \ | ||
3 1 | ||
Output: 7 | ||
Explanation: Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. | ||
``` | ||
|
||
**Example 2**: | ||
|
||
``` | ||
Input: [3,4,5,1,3,null,1] | ||
3 | ||
/ \ | ||
4 5 | ||
/ \ \ | ||
1 3 1 | ||
Output: 9 | ||
Explanation: Maximum amount of money the thief can rob = 4 + 5 = 9. | ||
``` | ||
|
||
## 题目大意 | ||
|
||
一个新的可行窃的地区只有一个入口,称之为“根”。 除了“根”之外,每栋房子有且只有一个“父“房子与之相连。一番侦察之后,聪明的小偷意识到“这个地方的所有房屋的排列类似于一棵二叉树”。 如果两个直接相连的房子在同一天晚上被打劫,房屋将自动报警。计算在不触动警报的情况下,小偷一晚能够盗取的最高金额。 | ||
|
||
|
||
## 解题思路 | ||
|
||
- 这一题是打家劫舍的第 3 题。这一题需要偷的房子是树状的。报警的条件还是相邻的房子如果都被偷了,就会触发报警。只不过这里相邻的房子是树上的。问小偷在不触发报警的条件下最终能偷的最高金额。 | ||
- 解题思路是 DFS。当前节点是否被打劫,会产生 2 种结果。如果当前节点被打劫,那么它的孩子节点可以被打劫;如果当前节点没有被打劫,那么它的孩子节点不能被打劫。按照这个逻辑递归,最终递归到根节点,取最大值输出即可。 | ||
|
||
## 代码 | ||
|
||
```go | ||
|
||
func rob337(root *TreeNode) int { | ||
a, b := dfsTreeRob(root) | ||
return max(a, b) | ||
} | ||
|
||
func dfsTreeRob(root *TreeNode) (a, b int) { | ||
if root == nil { | ||
return 0, 0 | ||
} | ||
l0, l1 := dfsTreeRob(root.Left) | ||
r0, r1 := dfsTreeRob(root.Right) | ||
// 当前节点没有被打劫 | ||
tmp0 := max(l0, l1) + max(r0, r1) | ||
// 当前节点被打劫 | ||
tmp1 := root.Val + l0 + r0 | ||
return tmp0, tmp1 | ||
} | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package leetcode | ||
|
||
// 解法一 DP | ||
func findTargetSumWays(nums []int, S int) int { | ||
total := 0 | ||
for _, n := range nums { | ||
total += n | ||
} | ||
if S > total || (S+total)%2 == 1 { | ||
return 0 | ||
} | ||
target := (S + total) / 2 | ||
dp := make([]int, target+1) | ||
dp[0] = 1 | ||
for _, n := range nums { | ||
for i := target; i >= n; i-- { | ||
dp[i] += dp[i-n] | ||
} | ||
} | ||
return dp[target] | ||
} | ||
|
||
// 解法二 DFS | ||
func findTargetSumWays1(nums []int, S int) int { | ||
// sums[i] 存储的是后缀和 nums[i:],即从 i 到结尾的和 | ||
sums := make([]int, len(nums)) | ||
sums[len(nums)-1] = nums[len(nums)-1] | ||
for i := len(nums) - 2; i > -1; i-- { | ||
sums[i] = sums[i+1] + nums[i] | ||
} | ||
res := 0 | ||
dfsFindTargetSumWays(nums, 0, 0, S, &res, sums) | ||
return res | ||
} | ||
|
||
func dfsFindTargetSumWays(nums []int, index int, curSum int, S int, res *int, sums []int) { | ||
if index == len(nums) { | ||
if curSum == S { | ||
*(res) = *(res) + 1 | ||
} | ||
return | ||
} | ||
// 剪枝优化:如果 sums[index] 值小于剩下需要正数的值,那么右边就算都是 + 号也无能为力了,所以这里可以剪枝了 | ||
if S-curSum > sums[index] { | ||
return | ||
} | ||
dfsFindTargetSumWays(nums, index+1, curSum+nums[index], S, res, sums) | ||
dfsFindTargetSumWays(nums, index+1, curSum-nums[index], S, res, sums) | ||
} |
Oops, something went wrong.