您的位置:首页 > 职场人生

Leetcode之Jump Game II 问题

2017-09-21 11:10 393 查看
问题描述:

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值的同步。

代码:




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java leetcode 算法 面试