jump game

中等 跳跃游戏
40%
通过

给出一个非负整数数组,你最初定位在数组的第一个位置。   

数组中的每个元素代表你在那个位置可以跳跃的最大长度。    

判断你是否能到达数组的最后一个位置。

您在真实的面试中是否遇到过这个题?  
哪家公司问你的这个题? 
感谢您的反馈
样例

A = [2,3,1,1,4],返回 true.

A = [3,2,1,0,4],返回 false.

动规解法:
public class Solution {
    /**
     * @param A: A list of integers
     * @return: The boolean answer
     */
    public boolean canJump(int[] A) {
        // wirte your code here
        boolean[] can = new boolean[A.length];
        //can[i] 表示能不能从前面跳到第i个位置
        //can[0] 是初始位置,永远能跳到
        can[0] = true;
        //遍历数组
        for (int i = 1; i < A.length; i++) {
            //遍历数组,看角标为i的元素是否能到达
            /**
             * A = [2,3,1,1,4]
             * i = 1 , j = 0, can[0] = true && 0+A[0] >=1 ==> can[1] = true;
             * 
             * i = 2 , j = 0, can[0] = true && 0+A[0] >=2 ==> can[2] = true; break;
             *         
             * i = 3 , j = 0, can[0] = true && 0+A[0] < 2;
             *         j = 1, can[1] = true && 1+A[1] >=3 ==> can[3] = true; break;
             *         
             * i = 4 , j = 0, can[0] = true && 0+A[0] < 2;
             *         j = 1, can[1] = true && 1+A[1] >=4 ==> can[4] = true; break;
             * 
             * A = [3,2,1,0,4]
             * i = 1 , j = 0, can[0] = true && 0+A[0] >=1 ==> can[1] = true;
             * 
             * i = 2 , j = 0, can[0] = true && 0+A[0] >=2 ==> can[2] = true; break;
             *         
             * i = 3 , j = 0, can[0] = true && 0+A[0] >=3 ==> can[3] = true; break;
             *         
             * i = 4 , j = 0, can[0] = true && 0+A[0] < 4;
             *         j = 1, can[1] = true && 1+A[1] < 4;
             *         j = 2, can[2] = true && 2+A[2] < 4;
             *         j = 3, can[3] = ture && 3+A[3] < 4;
             * ===> return false;
             * 
             */
             //看当前的i能否通过之前的位置到达
            for (int j = 0; j < i; j++) {
                if (can[j] && j + A[j] >= i) {
                    can[i] = true;
                    break;
                }
            }
        }
        
        return can[A.length - 1];
    }
}


贪心解法:
public class Solution {
    /**
     * @param A: A list of integers
     * @return: The boolean answer
     */
    public boolean canJump(int[] A) {
        // wirte your code here
        if(A.length == 0 || A == null){
            return false;
        }
        int farthest = A[0];
        for (int i = 1; i < A.length; i++) {
            //i<=farthest保证是在当前farthest的范围之内
            //A[i] + i >= farthest, 在当前farthest的范围内看能不能继续往后跳
            if (i <= farthest && A[i] + i >= farthest) {
                farthest = A[i] + i;
            }
        }
        //当遍历完了整个数组,看能不能跳到最后一位
        return farthest >= A.length - 1;
    }
}


上面的方法leetcode超时了,下面是贪心,没超时:

public class Solution {
    public boolean canJump(int[] nums) {
        if(nums == null || nums.length == 0){
            return false;
        }
        // reach 代表能到的最远距离
        int reach = 0;
        // i>reach 代表reach到不了i的位置,则循环结束
        for(int i = 0; i<=reach && i<nums.length; i++){
            reach = Math.max(reach,nums[i]+i);
        }
        //看reach能否到达末尾
        if(reach < nums.length-1){
            return false;
        }
        return true;
    }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值