代码随想录——跳跃游戏Ⅱ(Leetcode 45)

题目链接

贪心

java 复制代码
class Solution {
    public int jump(int[] nums) {
        if(nums.length == 1){
            return 0;
        }
        int count = 0;
        // 当前覆盖最远距离下标
        int curDistance = 0;
        // 下一步覆盖距离最远下标
        int nextDistance = 0;
        for(int i = 0; i <= nums.length; i++){
            nextDistance = Math.max(nums[i] + i, nextDistance);
            // 遇到当前覆盖最远距离下标
            if(i == curDistance){
                count++;
                // 更新当前覆盖最远距离下标
                curDistance = nextDistance;
                if(nextDistance >= nums.length - 1){
                    break;
                }
            }
        }
        return count;
    }
}
相关推荐
孤飞7 小时前
zero2Agent:面向大厂面试的 Agent 工程教程,从概念到生产的完整学习路线
算法
zjeweler7 小时前
“网安+护网”终极300多问题面试笔记-3共3-综合题型(最多)
笔记·网络安全·面试·职场和发展·护网行动
技术专家8 小时前
Stable Diffusion系列的详细讨论 / Detailed Discussion of the Stable Diffusion Series
人工智能·python·算法·推荐算法·1024程序员节
dllxhcjla8 小时前
微服务全套
java
Hacker_Nightrain8 小时前
详解Selenium 和Playwright两大框架的不同之处
自动化测试·软件测试·selenium·测试工具·职场和发展
csdn_aspnet8 小时前
C# (QuickSort using Random Pivoting)使用随机枢轴的快速排序
数据结构·算法·c#·排序算法
亚历克斯神8 小时前
JVM 内存管理 2026:深度解析与调优实战
java·spring·微服务
鹿角片ljp8 小时前
最长回文子串(LeetCode 5)详解
算法·leetcode·职场和发展
逻辑驱动的ken9 小时前
Java高频面试题:03
java·开发语言·面试·求职招聘·春招
广师大-Wzx9 小时前
一篇文章看懂MySQL数据库(下)
java·开发语言·数据结构·数据库·windows·python·mysql