Movatterモバイル変換


[0]ホーム

URL:


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

Commit5b67824

Browse files
committed
update the 45 problem
1 parent628c27d commit5b67824

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

‎src/0045.Jump-Game-II/Solution.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package Solution
22

33
import (
4+
"fmt"
45
"math"
56
)
67

@@ -54,6 +55,34 @@ func jump2(nums []int) int {
5455
returnres
5556
}
5657

58+
/**
59+
1
60+
61+
*/
62+
funcjump3(nums []int)int {
63+
//如果数组小于2,则说明不用跳跃返回0
64+
iflen(nums)<2 {
65+
return0
66+
}
67+
//当前可达到的最远位置,遍历过程中可达到最远位置,最小步数
68+
current_max_index,pre_max_max_index,jump_min:=nums[0],nums[0],1
69+
70+
fori:=1;i<len(nums);i++ {
71+
//如果无法向前移动才进行跳跃
72+
ifi>current_max_index {
73+
jump_min++
74+
//更新当前可以到达的最远位置
75+
current_max_index=pre_max_max_index
76+
}
77+
ifpre_max_max_index<nums[i]+i {
78+
//跟新最远位置
79+
pre_max_max_index=nums[i]+i
80+
}
81+
fmt.Println("i:",i,current_max_index,pre_max_max_index,jump_min)
82+
}
83+
returnjump_min
84+
}
85+
5786
funcMin(x,yint)int {
5887
ifx>y {
5988
returny

‎src/0045.Jump-Game-II/Solution_test.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ func TestSolution2(t *testing.T) {
3636
inputs []int
3737
expectint
3838
}{
39-
{"TestCacse 1", []int{2,3,1,1,4},2},
40-
{"TestCacse 1", []int{2},0},
41-
{"TestCacse 1", []int{10,3,4},1},
39+
{"TestCase 1", []int{2,3,1,1,4},2},
40+
{"TestCase 1", []int{2},0},
41+
{"TestCase 1", []int{10,3,4},1},
42+
{"TestCase 4", []int{4,1,1,3,1,1,1},2},
4243
}
4344

4445
//开始测试
@@ -53,6 +54,31 @@ func TestSolution2(t *testing.T) {
5354
}
5455
}
5556

57+
funcTestSolution3(t*testing.T) {
58+
//测试用例
59+
cases:= []struct {
60+
namestring
61+
inputs []int
62+
expectint
63+
}{
64+
{"TestCase 1", []int{2,3,1,1,4},2},
65+
{"TestCase 2", []int{2},0},
66+
{"TestCase 3", []int{10,3,4},1},
67+
{"TestCase 4", []int{4,1,1,3,1,1,1},2},
68+
}
69+
70+
//开始测试
71+
for_,c:=rangecases {
72+
t.Run(c.name,func(t*testing.T) {
73+
ret:=jump3(c.inputs)
74+
if!reflect.DeepEqual(ret,c.expect) {
75+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
76+
c.expect,ret,c.inputs)
77+
}
78+
})
79+
}
80+
}
81+
5682
//压力测试
5783
funcBenchmarkSolution(b*testing.B) {
5884

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp