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

Jump Game II
 1 public class Solution {
 2     public int jump(int[] A) {
 3         int len = A.length;
 4         if(len<=0 || len==1) return 0;
 5         int step =0;
 6         int next = 0;
 7         int cur =0;
 8         for(int i=0;i<len;i++){
 9             if(i>cur){
10                 step++;
11                 cur = next;
12             }
13             next = Math.max(next,i+A[i]);
14         }
15         return step;
16     }
17 }
View Code

Jump Game II

上一篇:【索引】Chapter 1. Algorithm Design :: Designing Efficient Algorithms :: Examples


下一篇:UIScrollView的分页和使用