45. 跳跃游戏 II

45. 跳跃游戏 II - 力扣(LeetCode)

java 复制代码
class Solution {
    public int jump(int[] nums) {
        int pos = nums.length - 1;//从后往前
        int ans = 0;//记录跳跃的次数
        while (pos > 0){
            //贪心的思想,找能跳到当前位置的距离最远的i,从左往右遍历
            for (int i = 0; i < pos; i++) {
                if (i + nums[i] >= pos){
                    pos = i;
                    ans++;
                    break;//找到了能跳到当前位置的距离最远的i,跳出本次循环
                }
            }
        }
        return ans;
    }
}
相关推荐
一灯架构6 小时前
90%的人答错!一文带你彻底搞懂ArrayList
java·后端
踏着七彩祥云的小丑6 小时前
pytest——Mark标记
开发语言·python·pytest
Dream of maid6 小时前
Python12(网络编程)
开发语言·网络·php
W23035765737 小时前
经典算法:最长上升子序列(LIS)深度解析 C++ 实现
开发语言·c++·算法
Y4090017 小时前
【多线程】线程安全(1)
java·开发语言·jvm
2401_892070987 小时前
链栈(链式栈) 超详细实现(C 语言 + 逐行精讲)
c语言·数据结构·链栈
不爱吃炸鸡柳7 小时前
Python入门第一课:零基础认识Python + 环境搭建 + 基础语法精讲
开发语言·python
布局呆星7 小时前
SpringBoot 基础入门
java·spring boot·spring
minji...8 小时前
Linux 线程同步与互斥(三) 生产者消费者模型,基于阻塞队列的生产者消费者模型的代码实现
linux·运维·服务器·开发语言·网络·c++·算法
Dxy12393102168 小时前
Python基于BERT的上下文纠错详解
开发语言·python·bert