Chinaunix首页 | 论坛 | 博客
  • 博客访问: 270083
  • 博文数量: 170
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1709
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-06 18:01
文章分类

全部博文(170)

文章存档

2016年(11)

2015年(130)

2014年(29)

分类: Java

2015-08-02 20:41:39


//Jump Game Total Accepted: 50023 Total Submissions: 185682 My Submissions Question Solution 
//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.
//
//Determine if you are able to reach the last index.
//
//For example:
//A = [2,3,1,1,4], return true.
//
//A = [3,2,1,0,4], return false.
import java.util.ArrayList;


public class JumpGame {


public static void main(String[] args) {
// TODO Auto-generated method stub


}


public boolean canJump(int[] nums) {


ArrayList<Integer> zeroindex=new ArrayList<>();
//只要管最后一个前面的 length-1的数
for(int i=0;i<nums.length-1;i++){
if(nums[i]==0){
zeroindex.add(i);
}
}

for(int i=0;i<zeroindex.size();i++){
int j;
for(j=zeroindex.get(i)-1;j>=0;j--){
if(nums[j]>zeroindex.get(i)-j)
break;
}
if(j<0)
return false;
}
return true;
}


}


//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 JumpGameII {


public static void main(String[] args) {
// TODO Auto-generated method stub


}


public int jump(int[] nums) {
if(nums==null||nums.length==0||nums.length==1)
return 0;
int jump=0;
int laststep=0;
for(int i=0;i<nums.length;i++){
nums[i]=nums[i]+i;
if(nums[i]>=nums.length-1){
jump++;
laststep=i;
//找到第一个就可以停止了
break;
}
}
while(laststep>0){
for(int j=0;j<laststep;j++){
if(nums[j]>=laststep){
jump++;
laststep=j;
break;
}

}
}
return jump;
}


}



阅读(276) | 评论(0) | 转发(0) |
0

上一篇:N-Queens

下一篇:Length of Last Word

给主人留下些什么吧!~~