@@ -205,66 +205,81 @@ class Solution {
205
205
```
206
206
207
207
### Python
208
-
208
+ 贪心(版本一)
209
209
``` python
210
210
class Solution :
211
- def jump (self , nums : List[int ]) -> int :
212
- if len (nums) == 1 : return 0
213
- ans = 0
214
- curDistance = 0
215
- nextDistance = 0
211
+ def jump (self , nums ):
212
+ if len (nums) == 1 :
213
+ return 0
214
+
215
+ cur_distance = 0 # 当前覆盖最远距离下标
216
+ ans = 0 # 记录走的最大步数
217
+ next_distance = 0 # 下一步覆盖最远距离下标
218
+
216
219
for i in range (len (nums)):
217
- nextDistance = max (i + nums[i], nextDistance)
218
- if i == curDistance:
219
- if curDistance != len (nums) - 1 :
220
- ans += 1
221
- curDistance = nextDistance
222
- if nextDistance >= len (nums) - 1 : break
220
+ next_distance = max (nums[i] + i, next_distance) # 更新下一步覆盖最远距离下标
221
+ if i == cur_distance: # 遇到当前覆盖最远距离下标
222
+ ans += 1 # 需要走下一步
223
+ cur_distance = next_distance # 更新当前覆盖最远距离下标(相当于加油了)
224
+ if next_distance >= len (nums) - 1 : # 当前覆盖最远距离达到数组末尾,不用再做ans++操作,直接结束
225
+ break
226
+
223
227
return ans
228
+
224
229
```
230
+ 贪心(版本二)
225
231
226
232
``` python
227
- # 贪心版本二
228
233
class Solution :
229
- def jump (self , nums : List[int ]) -> int :
230
- if len (nums) == 1 :
231
- return 0
232
- curDistance, nextDistance = 0 , 0
233
- step = 0
234
- for i in range (len (nums)- 1 ):
235
- nextDistance = max (nextDistance, nums[i]+ i)
236
- if i == curDistance:
237
- curDistance = nextDistance
238
- step += 1
239
- return step
234
+ def jump (self , nums ):
235
+ cur_distance = 0 # 当前覆盖的最远距离下标
236
+ ans = 0 # 记录走的最大步数
237
+ next_distance = 0 # 下一步覆盖的最远距离下标
238
+
239
+ for i in range (len (nums) - 1 ): # 注意这里是小于len(nums) - 1,这是关键所在
240
+ next_distance = max (nums[i] + i, next_distance) # 更新下一步覆盖的最远距离下标
241
+ if i == cur_distance: # 遇到当前覆盖的最远距离下标
242
+ cur_distance = next_distance # 更新当前覆盖的最远距离下标
243
+ ans += 1
244
+
245
+ return ans
246
+
240
247
```
248
+ 贪心(版本三) 类似‘55-跳跃游戏’写法
249
+
241
250
``` python
242
- # 贪心版本三 - 类似‘55-跳跃游戏’写法
243
251
class Solution :
244
252
def jump (self , nums ) -> int :
245
- if len (nums)== 1 : return 0
246
- i = 0
247
- count = 0
248
- cover = 0
249
- while i<= cover:
250
- for i in range (i,cover+ 1 ):
251
- cover = max (nums[i]+ i,cover)
252
- if cover>= len (nums)- 1 : return count+ 1
253
- count+= 1
253
+ if len (nums)== 1 : # 如果数组只有一个元素,不需要跳跃,步数为0
254
+ return 0
254
255
255
- ```
256
+ i = 0 # 当前位置
257
+ count = 0 # 步数计数器
258
+ cover = 0 # 当前能够覆盖的最远距离
259
+
260
+ while i <= cover: # 当前位置小于等于当前能够覆盖的最远距离时循环
261
+ for i in range (i, cover+ 1 ): # 遍历从当前位置到当前能够覆盖的最远距离之间的所有位置
262
+ cover = max (nums[i]+ i, cover) # 更新当前能够覆盖的最远距离
263
+ if cover >= len (nums)- 1 : # 如果当前能够覆盖的最远距离达到或超过数组的最后一个位置,直接返回步数+1
264
+ return count+ 1
265
+ count += 1 # 每一轮遍历结束后,步数+1
256
266
267
+
268
+ ```
269
+ 动态规划
257
270
``` python
258
- # 动态规划做法
259
271
class Solution :
260
272
def jump (self , nums : List[int ]) -> int :
261
- result = [10 ** 4 + 1 ]* len (nums)
262
- result[0 ]= 0
263
- for i in range (len (nums)):
264
- for j in range (nums[i]+ 1 ):
265
- if i+ j< len (nums): result[i+ j]= min (result[i+ j],result[i]+ 1 )
266
- # print(result) #打印数组
267
- return result[- 1 ]
273
+ result = [10 ** 4 + 1 ] * len (nums) # 初始化结果数组,初始值为一个较大的数
274
+ result[0 ] = 0 # 起始位置的步数为0
275
+
276
+ for i in range (len (nums)): # 遍历数组
277
+ for j in range (nums[i] + 1 ): # 在当前位置能够跳跃的范围内遍历
278
+ if i + j < len (nums): # 确保下一跳的位置不超过数组范围
279
+ result[i + j] = min (result[i + j], result[i] + 1 ) # 更新到达下一跳位置的最小步数
280
+
281
+ return result[- 1 ] # 返回到达最后一个位置的最小步数
282
+
268
283
269
284
```
270
285
0 commit comments