mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
Add solution 43、45、73、227
This commit is contained in:
21
leetcode/0045.Jump-Game-II/45. Jump Game II.go
Normal file
21
leetcode/0045.Jump-Game-II/45. Jump Game II.go
Normal file
@ -0,0 +1,21 @@
|
||||
package leetcode
|
||||
|
||||
func jump(nums []int) int {
|
||||
if len(nums) == 1 {
|
||||
return 0
|
||||
}
|
||||
needChoose, canReach, step := 0, 0, 0
|
||||
for i, x := range nums {
|
||||
if i+x > canReach {
|
||||
canReach = i + x
|
||||
if canReach >= len(nums)-1 {
|
||||
return step + 1
|
||||
}
|
||||
}
|
||||
if i == needChoose {
|
||||
needChoose = canReach
|
||||
step++
|
||||
}
|
||||
}
|
||||
return step
|
||||
}
|
47
leetcode/0045.Jump-Game-II/45. Jump Game II_test.go
Normal file
47
leetcode/0045.Jump-Game-II/45. Jump Game II_test.go
Normal file
@ -0,0 +1,47 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question45 struct {
|
||||
para45
|
||||
ans45
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para45 struct {
|
||||
nums []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans45 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem45(t *testing.T) {
|
||||
|
||||
qs := []question45{
|
||||
|
||||
{
|
||||
para45{[]int{2, 3, 1, 1, 4}},
|
||||
ans45{2},
|
||||
},
|
||||
|
||||
{
|
||||
para45{[]int{2, 3, 0, 1, 4}},
|
||||
ans45{2},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 45------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans45, q.para45
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, jump(p.nums))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
67
leetcode/0045.Jump-Game-II/README.md
Normal file
67
leetcode/0045.Jump-Game-II/README.md
Normal file
@ -0,0 +1,67 @@
|
||||
# [45. Jump Game II](https://leetcode.com/problems/jump-game-ii/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given an array of non-negative integers `nums`, you are initially positioned at the first index of the array.
|
||||
|
||||
Each element in the array represents your maximum jump length at that position.
|
||||
|
||||
Your goal is to reach the last index in the minimum number of jumps.
|
||||
|
||||
You can assume that you can always reach the last index.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: nums = [2,3,1,1,4]
|
||||
Output: 2
|
||||
Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: nums = [2,3,0,1,4]
|
||||
Output: 2
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `1 <= nums.length <= 1000`
|
||||
- `0 <= nums[i] <= 10^5`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个非负整数数组,你最初位于数组的第一个位置。数组中的每个元素代表你在该位置可以跳跃的最大长度。你的目标是使用最少的跳跃次数到达数组的最后一个位置。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 要求找到最少跳跃次数,顺理成章的会想到用贪心算法解题。扫描步数数组,维护当前能够到达最大下标的位置,记为能到达的最远边界,如果扫描过程中到达了最远边界,更新边界并将跳跃次数 + 1。
|
||||
- 扫描数组的时候,其实不需要扫描最后一个元素,因为在跳到最后一个元素之前,能到达的最远边界一定大于等于最后一个元素的位置,不然就跳不到最后一个元素,到达不了终点了;如果遍历到最后一个元素,说明边界正好为最后一个位置,最终跳跃次数直接 + 1 即可,也不需要访问最后一个元素。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
func jump(nums []int) int {
|
||||
if len(nums) == 1 {
|
||||
return 0
|
||||
}
|
||||
needChoose, canReach, step := 0, 0, 0
|
||||
for i, x := range nums {
|
||||
if i+x > canReach {
|
||||
canReach = i + x
|
||||
if canReach >= len(nums)-1 {
|
||||
return step + 1
|
||||
}
|
||||
}
|
||||
if i == needChoose {
|
||||
needChoose = canReach
|
||||
step++
|
||||
}
|
||||
}
|
||||
return step
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user