LeetCode——Jump Game
Description:
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.
For example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
贪心,每次取最大step看是否能够到达最后的索引。
public class Solution {
public boolean canJump(int[] nums) {
if(nums.length == 0 || nums.length == 1) return true;
int maxStep = nums[0];
for(int i=0; i<nums.length; i++) {
if(maxStep == 0) {
return false;
}
maxStep = Math.max(maxStep-1, nums[i]);
if(maxStep+i >= nums.length-1) {
return true;
}
}
return true;
}
}
LeetCode——Jump Game的更多相关文章
- [LeetCode] Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode——Jump Game II
Description: Given an array of non-negative integers, you are initially positioned at the first inde ...
- [leetcode]Jump Game @ Python
原题地址:https://oj.leetcode.com/problems/jump-game/ 题意: Given an array of non-negative integers, you ar ...
- LeetCode: Jump Game II 解题报告
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- LeetCode: Jump Game Total 解题报告
Jump GameGiven an array of non-negative integers, you are initially positioned at the first index of ...
- 动态规划小结 - 一维动态规划 - 时间复杂度 O(n),题 [LeetCode] Jump Game,Decode Ways
引言 一维动态规划根据转移方程,复杂度一般有两种情况. func(i) 只和 func(i-1)有关,时间复杂度是O(n),这种情况下空间复杂度往往可以优化为O(1) func(i) 和 func(1 ...
- [LeetCode] Jump Game II 贪心
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- nload 实时网速查看
nload eth0 -u K Device eth0 [192.168.0.33] (1/1):=================================================== ...
- vector的 emplace 和 insert 以及使用vector进行iterator遍历 且 erase的时候注意事项
vector<int> first;//Size()==2 first.push_back(); first.push_back(); //first.insert(2); vector& ...
- 在cxf中使用配置避免增加字段导致客户端必须更新、同步实体属性的问题
在使用cxf实现webservice时,经常碰到的问题就是如果在服务端,修改了一个接口的签名实现,如增加一个字段,或者删除一个字段.在这种情况下,在默认的配置中,就会报以下的错误信息: org.apa ...
- 了解 JavaScript (6)– 广告条(Banner)
在 Web 上冲浪时,常常会见到定期在图像之间切换的广告条.我们可以用 JavaScript 来实现,重复循环显示它们. 创建循环的广告条 RotatingBanner.html 页面中在循环的广告条 ...
- gpg的一些常用操作
(1)列出keys # gpg --list-keys /root/.gnupg/pubring.gpg ------------------------ pub 2048R/98681A63 2 ...
- mac上使用终端生成RSA公钥和密钥
首先确保你的电脑上安装了openssl,一般mac系统安装后都会自动安装!怎么安装??.....请百度...... 安装命令如下: sudo apt-get install openssl 在你的任何 ...
- 【Vegas原创】SVN的搭建及安装使用
中文手册:http://tortoisesvn.net/docs/nightly/TortoiseSVN_zh_CN/index.html 所需程序: 1,TortoiseSVN http://so ...
- 纯CSS实现图片抖动
实现方法: 1.将上文提到的以下JS内容删除: $(".imagelogo").mouseover(function() { obj = $(this); i = 5; timer ...
- Activity intent经常使用的 FLAG
Intent.FLAG_ACTIVITY_NEW_TASK 默认的跳转类型,会重新创建一个新的Activity,不过与这种情况,比方说Task1中有A,B,C三个Activity,此时在C中启动D的话 ...
- 【jquery】一款不错的音频播放器——Amazing Audio Player
前段时间分享了一款视频播放器,点击这里.今天介绍一款不错的音频播放器——Amazing Audio Player. 介绍: Amazing Audio Player 是一个使用很方便的 Windows ...