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 ...
随机推荐
- python3连接外部Mysql
前提条件,已经安装过MySQL(比如说以前web开发安装过MySQL) 1.安装PyMySQL pip install PyMySQL 2.测试 import pymysql as ps db = p ...
- 第二课补充01——redis-cli命令行详解、string类型、list类型、hash类型命令操作详解
一. redis-cli命令行参数 1.-x参数:从标准输入读取一个参数: [问题] [解决] 因为echo命令是默认带有回车\n的,不带回车需要echo –n命令: echo -n "ha ...
- Powershell Get-registerkey(susid)
$servers=get-content D:\serverregister.txt Get-registerkey -ComputerName $servers | select computer, ...
- 解决EF6中表名变成复数的情况
在用EF6 时,在进行数据调用的时候,总提示数据表名对象错误.. 解决此问题需要在继承DbContext的实体类中 加入: using System; using System.ComponentMo ...
- SEO优化 给a标签添加rel="nofollow"
为什么要使用nofollow标签? 我们使用nofollow标签的目的是很明确的,就是减少蜘蛛对页面上垃圾链接的爬行和传递权重,或者减少蜘蛛对页面上“无用”链接的爬行和传递链接权重. 这里所说的无用是 ...
- Struts 上传文件
1. 客户端注意事项 method="post" enctype="multipart/form-data" <input type="file ...
- 【零基础学习iOS开发】【02-C语言】08-基本运算
一.算术运算符 算术运算符很地简单.就是小学数学里面的一些加减乘除操作.只是呢.还是有一些语法细节须要注意的. 1.加法运算符 + 1 int a = 10; 2 3 int b = a + 5; 在 ...
- 【Sql Server】—sql Servler登录失败
登录失败报错信息如下: 标题: 连接到服务器 ------------------------------ 无法连接到 localhost. ----------------------------- ...
- 关闭SourceInsight的大括号自动缩进
使用Source Insight可以很好的管理项目代码,也非常便于阅读.但是,在使用Source Insight书写C语言代码时,会发现这样的问题,键入大括号之后,它会自动缩进一个制表符,这种处理跟我 ...
- 快速入门Python中文件读写IO是如何来操作外部数据的?
读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘, ...