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.)
思路:使用递归,始终去寻找最小的i能到目标位置。
class Counter { public static int counter; } public class Solution { public int jump(int[] A) { Counter.counter = 0; return jump(A, A.length - 1); } private int jump(int[] A, int end) { int i = 0; for (; i < end; i++) { if (i + A[i] >= end) { Counter.counter++; break; } } if (i == 0) return Counter.counter; jump(A, i); return Counter.counter; } }
原文地址:http://blog.csdn.net/mlweixiao/article/details/38874119