【Hot100】LeetCode—55. 跳跃游戏

目录

  • [1- 思路](#1- 思路)
  • [2- 实现](#2- 实现)
    • [⭐++55. 跳跃游戏++------题解思路](#⭐55. 跳跃游戏——题解思路)
  • [3- ACM 实现](#3- ACM 实现)


1- 思路

贪心

  • 利用 cover 记录覆盖的范围,每次覆盖范围移动一步更新 cover 并比较大小,如果可以移动则更新 cover

2- 实现

⭐++55. 跳跃游戏++------题解思路

java 复制代码
class Solution {
    public boolean canJump(int[] nums) {


        // 定义 cover
        int len = nums.length;

        int cover = nums[0];

        for(int i = 1 ; i < len;i++){
            if(--cover>=0){
                cover = Math.max(cover,nums[i]);
            }else{
                return false;
            }
        }
        return true;
    }
}

3- ACM 实现

java 复制代码
public class JumpGame {

    private static boolean jump(int[] nums){
        int cover = nums[0];
        for(int i = 1 ; i < nums.length ; i++){
            if(--cover >=0){
                cover = Math.max(cover,nums[i]);
            }else {
                return false;
            }
        }
        return true;
    }


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        String[] parts = input.split(" ");
        int[] nums = new int[parts.length];
        for(int i = 0 ;  i < parts.length;i++){
            nums[i] = Integer.parseInt(parts[i]);
        }

        System.out.println("结果是"+jump(nums));
    }
}
相关推荐
历程里程碑18 分钟前
LeetCode热题11:盛水容器双指针妙解
c语言·数据结构·c++·经验分享·算法·leetcode·职场和发展
wadesir7 小时前
Rust中的条件变量详解(使用Condvar的wait方法实现线程同步)
开发语言·算法·rust
yugi9878387 小时前
基于MATLAB实现协同过滤电影推荐系统
算法·matlab
TimberWill7 小时前
哈希-02-最长连续序列
算法·leetcode·排序算法
Morwit8 小时前
【力扣hot100】64. 最小路径和
c++·算法·leetcode
leoufung8 小时前
LeetCode 373. Find K Pairs with Smallest Sums:从暴力到堆优化的完整思路与踩坑
java·算法·leetcode
wifi chicken8 小时前
数组遍历求值,行遍历和列遍历谁更快
c语言·数据结构·算法
胡楚昊9 小时前
NSSCTF动调题包通关
开发语言·javascript·算法
Gold_Dino9 小时前
agc011_e 题解
算法
bubiyoushang8889 小时前
基于蚁群算法的直流电机PID参数整定 MATLAB 实现
数据结构·算法·matlab