本文主要是介绍TOP150-LC45-跳跃问题II-java版,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
java解法-贪心
/*跳跃游戏II
给定一个长度为 n 的 0 索引整数数组 nums。初始位置为 nums[0]。每个元素 nums[i] 表示从索引 i 向前跳转的最大长度。换句话说,如果你在 nums[i] 处,你可以跳转到任意 nums[i + j] 处:0 <= j <= nums[i]i + j < n
返回到达 nums[n - 1] 的最小跳跃次数。生成的测试用例可以到达 nums[n - 1]。*/
public class lc45 {public int jump(int[] nums) {int jumpCount = 0;int farthest = 0;int end = 0;int len = nums.length;for (int i = 0; i < len - 1; i++) {farthest = Math.max(farthest, i + nums[i]);if (end == i) {end = farthest;jumpCount++;}}return jumpCount;}
}
这篇关于TOP150-LC45-跳跃问题II-java版的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!