设为首页 加入收藏

TOP

LeetCode 44 Jump Game II
2015-07-20 17:48:17 来源: 作者: 【 】 浏览:2
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 23:48:36)
·如何使用python进行 (2025-12-24 23:48:34)
·python 爬虫入门该怎 (2025-12-24 23:48:31)
·Java 实现多个大文件 (2025-12-24 23:22:00)
·Java多线程编程在工 (2025-12-24 23:21:56)