当前位置:网站首页>LeetCode_ 55_ Jumping game
LeetCode_ 55_ Jumping game
2022-07-26 00:07:00 【Fitz1318】
Topic link
Title Description
Given an array of nonnegative integers nums , You're in the beginning of the array The first subscript .
Each element in the array represents the maximum length you can jump at that location .
Judge whether you can reach the last subscript .
Example 1:
Input :nums = [2,3,1,1,4]
Output :true
explain : You can jump first 1 Step , From the subscript 0 Reach subscript 1, And then from the subscript 1 jump 3 Step to the last subscript .
Example 2:
Input :nums = [3,2,1,0,4]
Output :false
explain : No matter what , Always arrive, subscript 3 The location of . But the maximum jump length of the subscript is 0 , So it's never possible to reach the last subscript .
Tips :
1 <= nums.length <= 3 * 10^40 <= nums[i] <= 10^5
Their thinking
The law of greed
- Take the maximum number of jump steps each time , Finally, if the total number of steps is greater than or equal to the length of the array , The explanation can
AC Code
class Solution {
public boolean canJump(int[] nums) {
int len = nums.length;
int jumpStep = 0;
if(len == 1){
return true;
}
for (int i = 0; i <= jumpStep; i++) {
jumpStep = Math.max(i + nums[i], jumpStep);
if (jumpStep >= len - 1) {
return true;
}
}
return false;
}
}
边栏推荐
猜你喜欢
随机推荐
Responsibility chain model of behavioral model
回溯——17. 电话号码的字母组合
STM32 pit encountered when using timer to do delay function
STM32 timer
BGR and RGB convert each other
Js理解之路:什么是原型链
STM32 serial port
“群魔乱舞”,牛市是不是结束了?2021-05-13
牛客/洛谷——[NOIP2003 普及组]栈
Binary tree -- 700. Search in binary search tree
Prometheus 运维工具 Promtool (二) Query 功能
07_ue4进阶_发射火球扣mp值和攻击扣血机制
@The underlying principle of Autowired annotation
Stack and queue - 150. Inverse Polish expression evaluation
URL address mapping configuration
Js理解之路:Js常见的6中继承方式
二叉树——700.二叉搜索树中的搜索
Shardingsphere data slicing
Compile live555 with vs2019 in win10
通货膨胀之下,后市如何操作?2021-05-14









