leetcode — jump-game-ii
2017-09-06 08:06
337 查看
/** * // Source : https://oj.leetcode.com/problems/jump-game-ii/ * * Created by lverpeng on 2017/7/17. * * 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.) * */ public class JumpGame2 { /** * 找到需要跳的次数最少的方法 * * 贪心算法:保证当前是是最优的,以期结果是最优的 * * 先保证能调到最后的最好的解,然后再计算能跳到当前最好解位置的最好解 * * @param arr * @return */ public int jump (int[] arr) { int end = arr.length - 1; int count = 0; while (end > 0) { for (int i = 0; i < end; i++) { if (i + arr[i] >= end) { count ++; end = i; break; } } } return count; } public static void main(String[] args) { JumpGame2 jumpGame2 = new JumpGame2(); int[] arr = new int[]{2,3,1,1,4}; System.out.println("2----->" + jumpGame2.jump(arr)); } }
相关文章推荐
- LeetCode | Jump Game II(跳跃游戏II)
- [LeetCode] Jump Game II
- LeetCodee 108 Jump Game II
- 【LeetCode】Jump Game II 跳跃游戏II - 贪心 Medium
- LeetCode 45 Jump Game II(按照数组进行移动)
- leetcode: Jump Game II
- Jump Game II - LeetCode
- Leetcode: Jump Game II
- 【leetcode】Jump Game I & II
- LeetCode Jump Game II
- [leetcode]Jump Game II
- leetcode 45. Jump Game II
- LeetCode-jump-game-ii
- [leetcode] Jump Game II
- leetcode-45 Jump Game II
- LeetCode-Jump Game II-跳跃游戏-BFS
- 【LeetCode】Jump Game II 解题报告
- LeetCode题目 Jump Game II
- [Leetcode] Jump Game II
- LeetCode第45之 Jump Game II