码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【LeetCode】No.55. Jump Game -- Java Version


    题目链接: https://leetcode.com/problems/jump-game/

    1. 题目介绍(Jump Game)

    You are given an integer array nums. You are initially positioned at the array’s first index, and each element in the array represents your maximum jump length at that position.

    【Translate】: 给你一个整数数组nums。初始位置是数组的第一个索引,数组中的每个元素表示在该位置的最大跳转长度。

    Return true if you can reach the last index, or false otherwise.

    【Translate】: 如果您能到达最后一个索引,则返回true,否则返回false。

    【测试用例】:
    testcase

    【约束】:
    Constraints

    2. 题解

    2.1 贪心 – O(n)

    原题解来自于hllowrld的 6 line java solution in O(n)。解题思路就是通过贪心的思想,找到当前的最大一跳,并判断当前的最大跳数是否能够到达下一目标,如果不行,则返回false.

        public boolean canJump(int[] nums) {
            int reachable = 0;
            for (int i=0; i<nums.length; ++i) {
                if (i > reachable) return false;
                reachable = Math.max(reachable, i + nums[i]);
            }
            return true;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    case1

    2.2 倒序遍历

    原题解来自于mlblount45的 Java 98% Percentile Solution。通过倒序检查元素,每当元素为0时,判断是否可以跳过,不能则返回false,能则下一跳。

    public class Solution {
        public boolean canJump(int[] nums) {
           if(nums.length < 2) return true;
           
           for(int curr = nums.length-2; curr>=0;curr--){
               if(nums[curr] == 0){
                   int neededJumps = 1;
                   while(neededJumps > nums[curr]){
                       neededJumps++;
                       curr--;
                       if(curr < 0) return false;
                   }
               }
           }
           return true;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    case2

  • 相关阅读:
    2、CAS详解
    【Java】Deque接口与List接口中的remove方法
    【英语:基础进阶_原著扩展阅读】J5.阅读分析中的常用概念
    如何在不同平台上搭建Flutter开发环境
    Vue--》过滤器介绍及其使用方法
    leetcode难度:困难773. 滑动谜题
    【Java】List、Set、数据结构、Collections
    12030.LMK03000时钟合成器
    数据结构与算法介绍与学习路线
    Ubuntu下安装node.js遇到的问题记录
  • 原文地址:https://blog.csdn.net/qq_41071754/article/details/125911037
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号