## Sunday, December 23, 2012

### [LeetCode] 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.)
» Solve this problem

[解题思路]

[Code]
``````1:    int jump(int A[], int n) {
2:      // Start typing your C/C++ solution below
3:      // DO NOT write int main() function
4:      int start = 0;
5:      int end = 0;
6:      int count =0;
7:      if(n == 1) return 0;
8:      while(end < n)
9:      {
10:        int max = 0;
11:        count++;
12:        for(int i =start; i<= end ; i++ )
13:        {
14:          if(A[i]+i >= n-1)
15:          {
16:            return count;
17:          }
18:          if(A[i]+ i > max)
19:            max = A[i]+i;
20:        }
21:        start = end+1;
22:        end = max;
23:      }
24:    }
``````

[注意]
Line7，当n=1的时候，需要特殊处理一下。