当前位置:网站首页>【刷题篇】跳跃游戏

【刷题篇】跳跃游戏

2022-06-09 21:55:00 m0_60631323

一、题目

OJ链接

给你一个非负整数数组 nums ,你最初位于数组的第一个位置。
数组中的每个元素代表你在该位置可以跳跃的最大长度。
你的目标是使用最少的跳跃次数到达数组的最后一个位置。
假设你总是可以到达数组的最后一个位置

二、题解

2.1思路

在这里插入图片描述

2.2 源码

 public int jump(int[] nums) {
    
         if(nums==null||nums.length==0){
    
             return 0;
         }
         int step=0;
         int cur=0;
         int next=0;
        for (int i = 0; i < nums.length; i++) {
    
            if(cur<i){
    
                step++;
                cur=next;
            }
            next=Math.max(next,i+nums[i]);
        }
        return step;
    }

原网站

版权声明
本文为[m0_60631323]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_60631323/article/details/125190980