当前位置:网站首页>Leetcode-55-jump game
Leetcode-55-jump game
2022-07-27 22:11:00 【benym】
# LeetCode-55- Jumping game
Given an array of nonnegative integers , You are first in the array .
Each element in the array represents the maximum length you can jump at that location .
Judge whether you can reach the last position .
Example 1:
Input : [2,3,1,1,4]
Output : true
explain : We can jump first 1 Step , From the position 0 arrive Location 1, And then from the position 1 jump 3 Step to the last position .Example 2:
Input : [3,2,1,0,4]
Output : false
explain : No matter what , You will always arrive at the index for 3 The location of . But the maximum jump length for this position is 0 , So you can never get to the last place .# Their thinking
Method 1、 greedy :
For any position in the array y, How to judge whether it can reach
According to the meaning , As long as there is one place x, It can be reached by itself , And the maximum length of its jump is x+nums[x], This value >=y, namely x+nums[x]>=y, So location y It can also reach
let me put it another way , For every accessible location x, It makes the x+1,x+2,......,x+nums[x] These continuous positions can reach
So we can record dynamically The farthest you can reach , For each take-off point , Update the corresponding The farthest you can reach
That is, try every point that can take off , Use max Indicates the farthest point that can be reached , exceed max You can't jump , Go straight back
# Java Code
class Solution {
public boolean canJump(int[] nums) {
boolean falg = true;
if(nums.length<2) return falg;
int max = 0;
for(int i=0;i<nums.length;i++){
if(i>max)
falg = false;
max = Math.max(max,i+nums[i]);
}
return falg;
}
}边栏推荐
- 九天后我们一起,聚焦音视频、探秘技术新发展
- An2021软件安装及基本操作(新建文件/导出)
- 二维数组的基本用法
- Apachespark command execution (cve-2022-33891) vulnerability recurrence
- Memo mode - unity
- 关系型数据库的设计思想,20张图给你看的明明白白
- Cocoapods reload
- Cloud native microservices Chapter 3: haproxy+kept
- It's too voluminous. A company has completely opened its core system (smart system) that has been operating for many years
- Simple use of enum
猜你喜欢
![Tencent cloud [hiflow] | automation --------- hiflow: still copying and pasting?](/img/dd/8ee989f5c9db632f78e79425497e71.png)
Tencent cloud [hiflow] | automation --------- hiflow: still copying and pasting?

How to customize logging of.Net6.0

华能福建公司与华为数字能源深化战略合作,共建低碳智能县域

Seven lines of code crashed station B for three hours

Lvs+kept highly available cluster

Simple use of enum

Are Transformers Effective for Time Series Forecasting?|填坑

Small change project (two versions) with detailed ideas
Excalidraw:很好用的在线、免费「手绘」虚拟白板工具

Why do server programs need to listen first
随机推荐
零钱通项目(两个版本)含思路详解
Pythia: Facebook's latest open source visual and language multitasking learning framework
An article takes you into the world of pycharm - stop asking me about pycharm installation and environment configuration!!!
Inventory Poka ecological potential project | cross chain characteristics to promote the prosperity of multi track
为什么服务端程序都需要先 listen 一下
Are Transformers Effective for Time Series Forecasting?|填坑
Go language learning notes - mutex start go language from scratch
对象在内存中存在形式&内存分配机制
B站崩了,那晚负责修复的开发人员做了什么?
直播软件app开发,uniapp scroll-view隐藏滚动条
Station B collapsed. What did the developer responsible for the repair do that night?
cocos:ccpDistance函数的简单运用以及实现眼球随着手指在眼眶中转动的功能
华能福建公司与华为数字能源深化战略合作,共建低碳智能县域
异常-Exception
Is log4j vulnerability still widespread?
Excalidraw: an easy-to-use online, free "hand drawn" virtual whiteboard tool
Small change project (two versions) with detailed ideas
Read Plato farm's eplato and the reason for its high premium
First zhanrui 5g chip! Exposure of Hisense F50, a pure domestic 5g mobile phone: equipped with Huben T710 + chunteng 510
成员方法及其传参机制