当前位置:网站首页>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;
}
}边栏推荐
- Pythia: Facebook's latest open source visual and language multitasking learning framework
- An2021 software installation and basic operation (new file / export)
- An article takes you into the world of pycharm - stop asking me about pycharm installation and environment configuration!!!
- 【StoneDB故障诊断】系统资源瓶颈诊断
- 软件测试的就业前景到底怎么样?
- How to deal with high concurrency deadlock?
- Common shortcut keys and setting methods of idea
- 每条你收藏的资讯背后,都离不开TA
- How to buy stocks on mobile phones? Is it safe to open an account
- 学完4种 Redis 集群方案要多久?我一口气给你说完
猜你喜欢

7行代码让B站崩溃3小时

day 1 - day 4

8000 word explanation of OBSA principle and application practice

Read Plato farm's eplato and the reason for its high premium

8000字讲透OBSA原理与应用实践

An2021 software installation and basic operation (new file / export)

Station B collapsed. What did the developer responsible for the repair do that night?
![[question 24] logic closed loop (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)](/img/c4/71a9933a3a1fdd14f84a41b640f5b5.jpg)
[question 24] logic closed loop (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)

vs2019 release模式调试:此表达式有副作用,将不予计算。

Reentranlock and source code analysis (learn ideas and click the source code step by step)
随机推荐
Finish learning redis cluster solution at one go
Exception -exception
matlab 绘制三坐标(轴)图
In depth understanding of recursive method calls (including instance maze problem, tower of Hanoi, monkey eating peach, fiboracci, factorial))
Station B collapsed. If we were the developer responsible for the repair that night
Nano semiconductor 65W gallium nitride (GAN) scheme was adopted by Xiaomi 10 Pro charger
Learn the use principle and core idea of thread pool from the source code
Member method and its parameter transmission mechanism
[Marine Science] climate indices data set
The US Department of justice added 16 new charges against Huawei, including stealing trade secrets
Inventory Poka ecological potential project | cross chain characteristics to promote the prosperity of multi track
How to learn object Defineproperty | an article takes you to quickly learn
If demand splitting is as simple as cutting a cake | agile practice
一口气学完 Redis 集群方案
Inertial navigation principle (VII) -imu error classification (II) -allan variance analysis method +imu test + calibration introduction
Software testing interview question: what is the focus of unit testing, integration testing, and system testing?
Leetcode 301. delete invalid parentheses
只会Excel想做图表可视化,让数据动起来?可以,快来围观啦(附大量模板下载)
【OBS】P B 丢帧阈值 buffer_duration_usec
Regular expression exercise