Leetcode #55. Jump Game 跳跃游戏 解题报告

1 解题思想

这道题的意思,就是给了一个数组,数组里面表示的是在这个位置上,最大可跳跃的位置。
那么现在,就假设你在0的位置上,问你可以跳跃到最末尾的位置吗?

那么这道题的思路就是使用一个贪心法,使用一个步进指针,用一个上界指针。
每次遍历的时候,不停的更新上界指针的位置(也就是当前位置+当前可以跳到的位置),知道看你能遇到结尾吗?如果成功了,就范围true,没有就返回false

2 原题

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.

3 AC解

public class Solution {
    /**
     * 贪心寻找上界!!!!还有这题是不寻找路线的!!!
     * */
    public boolean canJump(int[] nums) {
        int reach=0;
        int i=0;
        while(i<nums.length && i<=reach){
            reach=Math.max(reach,i+nums[i]);
            i++;
        }
        return reach>=nums.length-1;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值