当前位置:网站首页>LeetCode_55_跳跃游戏
LeetCode_55_跳跃游戏
2022-07-25 23:58:00 【Fitz1318】
题目链接
题目描述
给定一个非负整数数组 nums ,你最初位于数组的 第一个下标 。
数组中的每个元素代表你在该位置可以跳跃的最大长度。
判断你是否能够到达最后一个下标。
示例 1:
输入:nums = [2,3,1,1,4]
输出:true
解释:可以先跳 1 步,从下标 0 到达下标 1, 然后再从下标 1 跳 3 步到达最后一个下标。
示例 2:
输入:nums = [3,2,1,0,4]
输出:false
解释:无论怎样,总会到达下标为 3 的位置。但该下标的最大跳跃长度是 0 , 所以永远不可能到达最后一个下标。
提示:
1 <= nums.length <= 3 * 10^40 <= nums[i] <= 10^5
解题思路
贪心法
- 每次取最大跳跃步数,最后如果总步数大于等于数组长度,说明可以
AC代码
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;
}
}
边栏推荐
- Iterator pattern of behavioral pattern
- [ManageEngine] servicedesk plus won the 2022 safety model engineering data safety award
- Observer model of behavioral model
- What is multithreading
- Responsibility chain model of behavioral model
- 浅识 OWASP
- C language implementation of three chess
- 老旧笔记本电脑变服务器(笔记本电脑+内网穿透)
- 统计之歌 歌词
- 二叉树——226. 翻转二叉树
猜你喜欢

NVIDIA cuDNN学习

痞子衡嵌入式:MCUXpresso IDE下将源码制作成Lib库方法及其与IAR,MDK差异

Zhiniu stock -- 09

Part 74: overview of machine learning optimization methods and superparameter settings

S4/hana ME21N create Po output control message button missing solution (switch EDI output mode brf+ to Nast mode)

The GUI interface of yolov3 (3) -- solve the out of memory problem and add camera detection function

Leetcode200-查找岛屿数量详解

Payment terms in SAP message No. vg202 IDoc e1edk18 have been transferred: check data

C语言实战之猜拳游戏

Practical experience of pair programming
随机推荐
二叉树——654. 最大二叉树
A brief introduction to OWASP
Exercise (1) create a set C1 to store the elements "one", "two", "three"“
【英雄哥七月集训】第 24天: 线性树
Song of statistics lyrics
Are you still using your browser's own bookmarks? This bookmark plugin is awesome
Read the field status of account in ABAP code (hidden, optional, required)
Solve the problem of rapid index bar extrusion
The late Apple co-founder Steve Jobs was posthumously awarded the U.S. presidential medal of freedom
Android solves the risk of database injection vulnerability
Array merge method: concat()
What is inode? It will help you understand inode and what help inode provides when creating and deleting files.
Typescript TS basic knowledge and so on
二叉树——226. 翻转二叉树
死信队列 和消息TTL过期代码
模式之固定与交替顺序执行
Article 75: writing skills of academic papers
1223. Dice simulation range DP
Key features and application trends of next generation terminal security management
Scroll series