Skip to content

Commit 13d26dd

Browse files
aQuaaQua
aQua
authored and
aQua
committed
55 accepted
1 parent 5af0fee commit 13d26dd

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

Algorithms/0055.jump-game/README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,15 @@
33
## 题目
44
Given an array of non-negative integers, you are initially positioned at the first index of the array.
55

6-
76
Each element in the array represents your maximum jump length at that position.
87

9-
108
Determine if you are able to reach the last index.
119

12-
13-
10+
```
1411
For example:
1512
A = [2,3,1,1,4], return true.
16-
17-
1813
A = [3,2,1,0,4], return false.
14+
```
1915
## 解题思路
2016

2117
见程序注释
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
package Problem0055
22

33
func canJump(nums []int) bool {
4+
i := len(nums) - 2
5+
for ; i >= 0; i-- {
6+
if nums[i] != 0 {
7+
continue
8+
}
49

10+
j := i - 1
11+
for ; j >= 0; j-- {
12+
if i-j < nums[j] {
13+
i = j
14+
break
15+
}
16+
}
17+
18+
if j == -1 {
19+
return false
20+
}
21+
}
22+
23+
return true
524
}

Algorithms/0055.jump-game/jump-game_test.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package Problem0055
22

33
import (
4-
"testing"
54
"fmt"
5+
"testing"
66

77
"github.com/stretchr/testify/assert"
88
)
@@ -19,7 +19,7 @@ type para struct {
1919

2020
// ans 是答案
2121
type ans struct {
22-
one bool
22+
one bool
2323
}
2424

2525
func Test_Problem0055(t *testing.T) {
@@ -29,20 +29,38 @@ func Test_Problem0055(t *testing.T) {
2929

3030
question{
3131
para{
32-
,
32+
[]int{2, 3, 1, 1, 4},
33+
},
34+
ans{
35+
true,
36+
},
37+
},
38+
39+
question{
40+
para{
41+
[]int{3, 2, 1, 0, 4},
3342
},
3443
ans{
35-
,
44+
false,
3645
},
3746
},
38-
47+
48+
question{
49+
para{
50+
[]int{3, 2, 2, 0, 4},
51+
},
52+
ans{
53+
true,
54+
},
55+
},
56+
3957
// 如需多个测试,可以复制上方元素。
4058
}
4159

4260
for _, q := range qs {
4361
a, p := q.ans, q.para
4462
fmt.Printf("~~%v~~\n", p)
4563

46-
ast.Equal(a.one, canJump(p ), "输入:%v", p)
64+
ast.Equal(a.one, canJump(p.nums), "输入:%v", p)
4765
}
4866
}

0 commit comments

Comments
 (0)