LeetCode第[55]题(Java):Jump Game
题目:跳跳游戏
难度:Medium
题目内容:
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
翻译:
给定一个非负整数数组,您最初被定位在数组的第一个索引中。
数组中的每个元素代表您在该位置的最大跳跃长度。
确定你是否能够到达最后一个索引。
Example 1:
Input: [2,3,1,1,4]
Output: true
Explanation: 第一步,跳2步到索引2,第二步跳1步到索引3,第三步跳1步到索引4,到达
第一步,跳1步到索引1,第二步跳3步到索引4,到达
Example 2:
Input: [3,2,1,0,4]
Output: false
Explanation: 无论如何,你总是会到达索引3。它的最大跳转长度为0,这使得到达最后一个索引是不可能的。
我的思路:不知道咋做。。。
答案代码:
public boolean canJump(int[] A) {
int max = 0;
for(int i=0;i<A.length;i++){
if(i>max) {return false;}
max = Math.max(A[i]+i,max);
}
return true;
}
答案思路:因为所跳的步数能从0到A[ i ] 都行所以只要前面能达到的最远下标的最大值能大于后面的下标,就表示能跳到,否则跳不到。能达到的最远下标为A[i] + i 。
eg.:[3,2,1,0,4]
前面三个的能达到的最远下标最大值为3,所以此时最远只到下标3,而下标3的最远下标是自己,到了下标4进行判断发现前面的最远下标比自己的下标小,所以跳不到。
LeetCode第[55]题(Java):Jump Game的更多相关文章
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- LeetCode第[1]题(Java):Two Sum 标签:Array
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[4]题(Java):Median of Two Sorted Arrays 标签:Array
题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...
- LeetCode第[29]题(Java):Divide Two Integers
题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...
- LeetCode第[4]题(Java):Median of Two Sorted Arrays (俩已排序数组求中位数)——HARD
题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...
- LeetCode第[15]题(Java):3Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c ...
- LeetCode第[16]题(Java):3Sum Closest 标签:Array
题目难度:Medium 题目: Given an array S of n integers, find three integers in S such that the sum is closes ...
随机推荐
- 如何看懂ORACLE执行计划
如何看懂Oracle执行计划 一.什么是执行计划 An explain plan is a representation of the access path that is taken when a ...
- 编译型 解释型 C++工作原理
C++教程_w3cschool https://www.w3cschool.cn/cpp/ C++工作原理: C++语言的程序因为要体现高性能,所以都是编译型的.但其开发环境,为了方便测试,将调试环境 ...
- c++相关知识
0.C语言基础知识及系统相关:http://c.biancheng.net/cpp/u/jiaocheng/ 1.C++ include观点与机制:http://developer.51cto.com ...
- Spring 的IOC容器之注解的方式
1. 环境搭建 1.1 导入所需 jar 包 引入 IOC 容器必须的6个jar包; spring-aop-4.3.10.RELEASE.jar, Spring 框架的AOP的jar包; 1.2 创建 ...
- MySQL给字段唯一索引的三种方法
建表时添加 DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `stu_id` ) NOT NULL AUTO_INCREMENT, ` ...
- WTForms In Flask(WTForms在Flask中的应用)
WTForms WTForms是一个支持多个web框架的form组件,主要用于对用户请求数据进行验证. 安装wtforms : pip3/pip install wtforms 用户登录/注册示例 项 ...
- 我的Android进阶之旅------>关于使用Android Studio替换App的launcher图标之后仍然显示默认的ic_launcher图标的解决方法
前言 最近做了一个App,之前开发该App的时候一直以来都是默认的launcher图标启动的, 今天美工换了一个App的launcher 图标,因此在Android Studio中将默认的lanche ...
- Linux命令之pip
使用pip进行install,sudo pip install xxx 使用pip进行update,sudo pip install --update xxx 使用pip设置超时时间,sudo pip ...
- sql server如何把查询结果发邮件出去
原本:https://zhidao.baidu.com/question/1819725575342685788.html --1.启用Database Mail扩展存储过程 sp_configure ...
- beego——模板函数
beego 支持用户定义模板函数,但是必须在 beego.Run() 调用之前,设置如下: func hello(in string)(out string){ out = in + "wo ...