当前位置:网站首页>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;
}
}
边栏推荐
- 二叉树——530.二叉搜索树的最小绝对差
- STM32 pit encountered when using timer to do delay function
- 你还在用浏览器自带书签?这款书签插件超赞
- Niuke / Luogu - [noip2003 popularization group] stack
- FreeRTOS个人笔记-互斥量
- 07_ue4进阶_发射火球扣mp值和攻击扣血机制
- Piziheng embedded: the method of making source code into lib Library under MCU Xpress IDE and its difference with IAR and MDK
- Detailed explanation of kubernetes network plug-ins - calico chapter - Overview
- 通货膨胀之下,后市如何操作?2021-05-14
- 二叉树——226. 翻转二叉树
猜你喜欢

Binary tree - 617. Merge binary tree

bond网卡模式配置

Solve the problem of rapid index bar extrusion

BOM 浏览器对象模型

Binary tree - 404. Sum of left leaves

Binary tree - 226. Flip binary tree

Binary tree - 110. Balanced binary tree

Compile live555 with vs2019 in win10

Article 75: writing skills of academic papers

Shardingsphere data slicing
随机推荐
牛市还没有结束,还有下半场 2021-05-18
Annotation @autowired source code analysis
初阶C语言 - 分支语句(if、switch)
二叉树——104. 二叉树的最大深度
LeetCode 刷题系列 -- 931. 下降路径最小和
SQLZOO——Nobel Quiz
Leetcode107-二叉树的层序遍历II详解
二叉树——404. 左叶子之和
Yes, UDP protocol can also be used to request DNS server
二叉树——110. 平衡二叉树
MySQL——数据库日志
Piziheng embedded: the method of making source code into lib Library under MCU Xpress IDE and its difference with IAR and MDK
[brother hero July training] day 24: linear tree
Fixed and alternate sequential execution of modes
Binary tree - 530. Minimum absolute difference of binary search tree
Js理解之路:Object.call与Object.create()实现继承的原理
How to use yolov5 as an intelligent transportation system for red light running monitoring (1)
没错,请求DNS服务器还可以使用UDP协议
【英雄哥七月集训】第 24天: 线性树
网站服务器停止响应是什么意思?