Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e07a3ca

Browse files
Merge pull request youngyangyang04#1279 from Frankheartusf/master
更新了0045 跳跃游戏II golang代码
2 parents 42c5fa7 + 3018a83 commit e07a3ca

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

‎problems/0045.跳跃游戏II.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -217,18 +217,26 @@ class Solution:
217217
### Go
218218
```Go
219219
func jump(nums []int) int {
220-
dp:=make([]int ,len(nums))
221-
dp[0]=0
222-
223-
for i:=1;i<len(nums);i++{
224-
dp[i]=i
225-
for j:=0;j<i;j++{
226-
if nums[j]+j>i{
227-
dp[i]=min(dp[j]+1,dp[i])
228-
}
229-
}
230-
}
231-
return dp[len(nums)-1]
220+
dp := make([]int, len(nums))
221+
dp[0] = 0//初始第一格跳跃数一定为0
222+
223+
for i := 1; i < len(nums); i++ {
224+
dp[i] = i
225+
for j := 0; j < i; j++ {
226+
if nums[j] + j >= i {//nums[j]为起点,j为往右跳的覆盖范围,这行表示从j能跳到i
227+
dp[i] = min(dp[j] + 1, dp[i])//更新最小能到i的跳跃次数
228+
}
229+
}
230+
}
231+
return dp[len(nums)-1]
232+
}
233+
234+
func min(a, b int) int {
235+
if a < b {
236+
return a
237+
} else {
238+
return b
239+
}
232240
}
233241
```
234242

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /