Algorithms—45.Jump Game II
2015-09-07 14:49
543 查看
思路:动态规划。
public class Solution {
public int jump(int[] nums) {
if (nums.length==1) {
return 0;
}
int max=0;
int[] steps=new int[nums.length];
steps[0]=0;
for (int i = 0; i < nums.length-1; i++) {
int n=nums[i];
if (n+i>max) {
int s=steps[i];
for (int j = max+1; j <=n+i; j++) {
if (j<steps.length) {
if (steps[j]==0||steps[j]>s+1) {
steps[j]=s+1;
}
}
}
max=n+i;
}
}
return steps[steps.length-1];
}
}
public class Solution {
public int jump(int[] nums) {
if (nums.length==1) {
return 0;
}
int max=0;
int[] steps=new int[nums.length];
steps[0]=0;
for (int i = 0; i < nums.length-1; i++) {
int n=nums[i];
if (n+i>max) {
int s=steps[i];
for (int j = max+1; j <=n+i; j++) {
if (j<steps.length) {
if (steps[j]==0||steps[j]>s+1) {
steps[j]=s+1;
}
}
}
max=n+i;
}
}
return steps[steps.length-1];
}
}
相关文章推荐
- GO环境设置
- GO备忘录
- 1101 Oh, my God!【错排】
- 【最近公共祖先】[COCI]STOGOVI
- Algorithm --> 爬楼梯求最大分数
- [FFmpeg] 去除 logo
- Algorithms—39.Combination Sum
- google.com 还会换IP
- IE和Google浏览器事件传递参数不同
- Algorithm --> 棋盘中求出A到B的最小步数
- Algorithm --> 求N以内的真分数个数
- django给admin增加功能
- 悟空:用Go语言编写的全文搜索引擎
- 类别 category 以及类扩展
- 认识理解zygote
- 【哥德巴赫猜想】POJ Goldbach's Conjecture 2262
- django中给ajax提交加上csrf
- 【哥德巴赫猜想】LightOJ Goldbach`s Conjecture 1259
- Uva 11292 The Dragon of Loowater
- Intent中的四个重要属性——Action、Data、Category、Extras