Leetcode之Jump Game II 问题

来源:互联网 发布:2万工资的java程序员 编辑:程序博客网 时间:2024/05/22 03:25

问题描述:

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Your goal is to reach the last index in the minimum number of jumps.

示例:

For example:
Given array A = [2,3,1,1,4]

The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)

问题来源:Jump Game II (详细地址:https://leetcode.com/problems/jump-game-ii/description/)

思路分析:

这道题相比Jump Game 来讲,只是要求了步数最少,即到达数组末尾要求移动最少的步数,所以在这多了个步数的计算,但是应该怎么计算才合理呢?答案就是当i到达了上一步中的global的值的时候,就需要开始加一步了,也就是一步要走到global的尽头才行,所以还需要保存一个变量,用来保持步数(count)和global值的同步。

代码: