File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -142,6 +142,7 @@ public:
142
142
143
143
### Java
144
144
``` Java
145
+ // 版本一
145
146
class Solution {
146
147
public int jump (int [] nums ) {
147
148
if (nums == null || nums. length == 0 || nums. length == 1 ) {
@@ -172,7 +173,30 @@ class Solution {
172
173
}
173
174
```
174
175
176
+ ``` java
177
+ // 版本二
178
+ class Solution {
179
+ public int jump (int [] nums ) {
180
+ int result = 0 ;
181
+ // 当前覆盖的最远距离下标
182
+ int end = 0 ;
183
+ // 下一步覆盖的最远距离下标
184
+ int temp = 0 ;
185
+ for (int i = 0 ; i <= end && end < nums. length - 1 ; ++ i) {
186
+ temp = Math . max(temp, i + nums[i]);
187
+ // 可达位置的改变次数就是跳跃次数
188
+ if (i == end) {
189
+ end = temp;
190
+ result++ ;
191
+ }
192
+ }
193
+ return result;
194
+ }
195
+ }
196
+ ```
197
+
175
198
### Python
199
+
176
200
``` python
177
201
class Solution :
178
202
def jump (self , nums : List[int ]) -> int :
You can’t perform that action at this time.
0 commit comments