[Leetcode 55]跳格子JumpGame

【题目】

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.

【举例】

Example 1:

Input: [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2:

Input: [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum

【思路】

贪心、取最大

【代码】

class Solution {
public boolean canJump(int[] nums) {

扫描二维码关注公众号,回复: 3747786 查看本文章

  return go(nums,0);

}
public boolean go(int[] nums,int index){
  int len=nums.length-1;
    if(index==len)
    return true;
  int far=Math.min(len,index+nums[index]);
  for(int next=index+1;next<far;next++){
    go(nums,next);
    return true;
  }
return false;
}
}

【其他】

猜你喜欢

转载自www.cnblogs.com/inku/p/9860398.html
今日推荐