###### leetcode 45：Jump Game II

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.)

//参考 https://github.com/soulmachine/leetcode
int max(int a , int b){
return a > b ? a : b ;
}

int jump(int* nums, int numsSize) {
int maxReach=0;//站在[0,i]区间里面跳一步可以到达的最远距离
int curReach=0;//目前跳ret步，可以到达的长度
int ret=0;
for (int i = 0; i < numsSize; i++)
{
if(curReach=numsSize-1)
return ret;
}
return 0;
}

// LeetCode, Jump Game II
// 时间复杂度O(n)，空间复杂度O(1)
class Solution {
public:
int jump(int A[], int n) {
int step = 0; // 最小步数
int left = 0;
int right = 0; // [left, right] 是当前能覆盖的区间
if (n == 1) return 0;
while (left <= right) { // 尝试从每一层跳最远
++step;//在[left, right]区间 跳一步
const int old_right = right;
// 跳着一步最大可能跳到的区间
for (int i = left; i <= old_right; ++i) {
int new_right = i + A[i];
if (new_right >= n - 1) return step;
if (new_right > right) right = new_right;
}
left = old_right + 1;
}
return 0;
}
};

// 动态规划
int jump(int* nums, int numsSize) {
int *pStepFlags = (int *)malloc(numsSize*sizeof(int)); // 到 i 最少跳多少步
pStepFlags[0] = 0;
memset(pStepFlags + 1, 0x7f, (numsSize - 1)*sizeof(int));

for (int i = 1; i= i &&  StepCount < pStepFlags[i])
{
pStepFlags[i] = StepCount;
break;// 不要再循环了，前面的一定比后面的步数少！！！！Each element in the array represents your maximum jump length at that position，因为每个序列表示最大可以跳的距离，可以少跳
}
}
}
return pStepFlags[numsSize - 1];
}

#### LeetCode --- 45. Jump Game II

2015-02-10 08:59:21

#### [leetcode]45. Jump Game II 跳棋游戏2 C++/PYTHON实现【hard难度】

2016-06-17 10:15:08

#### leetcode解题之45. Jump Game II&55. Jump Game java (跳跃游戏)

2017-04-09 01:09:19

#### [leetcode-45]Jump Game II(java)

2015-10-13 16:24:45

#### 【LeetCode】Jump Game II 解题报告

2014-12-09 17:17:02

#### LeetCode_Jump Game&&Jump Game II

2014-08-09 12:46:38

#### Jump Game II -- LeetCode

2014-03-20 23:25:59

#### LeetCode | Jump Game II

2013-09-15 21:26:56

#### Jump Game II (最小步数到达终点，贪心) 【leetcode】

2013-09-20 19:33:17

#### 55. Jump Game Leetcode Python

2015-03-03 06:24:01

## 不良信息举报

leetcode 45：Jump Game II