您的位置:首页 > 其它

leetcode 55. Jump Game

2017-01-06 09:30 393 查看
参考了http://www.cnblogs.com/zichi/p/4808025.html

遍历数组,不断寻找能到达的最右端的位置,直到下标比最右端位置大为止。最后判断是否能到达最后一个位置。

public class Solution {

/*
* 	public boolean canJump(int[] nums) {
int i;
int rightMost = 1;

if(nums.length==0 || nums == null)
return false;

for(i=0;i<nums.length;i++)
{
if(i+1>rightMost)
break;
rightMost = Math.max(rightMost, i+1+nums[i]);
}

if(rightMost >= nums.length)
return true;
else
return false;
}
*/
public boolean canJump(int[] nums) {
int i;
int rightMost = 0;

if(nums.length==0 || nums == null)
return false;

for(i=0;i<nums.length;i++)
{
if(i>rightMost)
break;
rightMost = Math.max(rightMost, i+nums[i]);
}

if(rightMost >= nums.length-1)
return true;
else
return false;
}

public static void main(String args[])
{
//int[] nums = {3,2,1,1,4};
int[] nums = {0};
boolean res = new Solution().canJump(nums);
System.out.println(res);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: