设为首页 加入收藏

TOP

LeetCode 44 Jump Game II
2015-07-20 17:48:17 来源: 作者: 【 】 浏览:1
Tags:LeetCode Jump Game

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;
	}
}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU2546-饭卡(DP+贪心) 下一篇C++技术问题总结-第7篇 map、vect..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Python中文网 - 人生 (2025-12-24 18:49:47)
·【整整648集】这绝对 (2025-12-24 18:49:44)
·Python超详细一条龙 (2025-12-24 18:49:42)
·【超详细】JDK 下载 (2025-12-24 18:19:32)
·Java_百度百科 (2025-12-24 18:19:29)