【LeetCode-45】Jump Game II

来源:互联网 发布:微信公众号对接数据库 编辑:程序博客网 时间:2024/04/30 02:50

运用了动态规划的思想,希望不要被我写的代码搞糊涂

public class JumpGameII {public int jump(int[] nums) {if(nums == null || nums.length == 0){return 0;}int[] steps = new int[nums.length];Arrays.fill(steps, Integer.MAX_VALUE);steps[0] = 0;//先执行一遍,这种书写方式要记着int j = 0;for(int i = 0;i < steps.length;){for(;j <= nums[i] + i && j < steps.length;j ++){if(steps[i] + 1 < steps[j]){steps[j] = steps[i] + 1;}}i ++;j = nums[i - 1] + i; }return steps[steps.length - 1];    }}


0 0
原创粉丝点击