当前位置:网站首页>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;
}
}
边栏推荐
- 没错,请求DNS服务器还可以使用UDP协议
- Sequence traversal II of leetcode107 binary tree
- Getting started with Servlet
- 行为型模式之观察者模式
- BGR and RGB convert each other
- 栈与队列——150. 逆波兰表达式求值
- Leetcode107-二叉树的层序遍历II详解
- GUI interface of yolov3 (2) -- beautify the page + output the name and quantity of the identified object
- C语言实战之猜拳游戏
- 注解@Autowired源码解析
猜你喜欢

【一库】mapbox-gl!一款开箱即用的地图引擎

Binary tree - 530. Minimum absolute difference of binary search tree

C语言实战之猜拳游戏

SHIB(柴犬币)一月涨幅数百倍,百倍币需具备哪些核心要素?2021-05-09

Binary tree -- 104. Maximum depth of binary tree

GUI interface of yolov3 (2) -- beautify the page + output the name and quantity of the identified object

Redirection and request forwarding

Piziheng embedded: the method of making source code into lib Library under MCU Xpress IDE and its difference with IAR and MDK

复盘:推荐系统—— 负采样策略

06_ UE4 advanced_ Set up a large map using the terrain tool
随机推荐
BOM 浏览器对象模型
二叉树——654. 最大二叉树
初阶C语言 - 分支语句(if、switch)
bond网卡模式配置
Sequence traversal II of leetcode107 binary tree
回溯——17. 电话号码的字母组合
STM32 pit encountered when using timer to do delay function
BGR and RGB convert each other
Exercise (1) create a set C1 to store the elements "one", "two", "three"“
本轮牛市还能持续多久?|疑问解答 2021-05-11
Redirection and request forwarding
Cherish time and improve efficiency
Piziheng embedded: the method of making source code into lib Library under MCU Xpress IDE and its difference with IAR and MDK
What does it mean that the web server stops responding?
Leetcode question brushing series -- 931. Minimum sum of descent path
注解@Autowired源码解析
YoloV4-tiny网络结构
最近随感,关于牛市和DeFi 2021-05-17
多御安全浏览器手机版将增加新功能,使用户浏览更个性化
二叉树——110. 平衡二叉树