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 ...
随机推荐
- WPS 文字排版 标题回车后标题号自动增加
飞思卡来互联网提供全球性的 看了看 风格的呵呵 合格否d合格否的 secure embedded嵌入式解 个国家和地区,注册了成千上万项专利,产品面向 物联网,汽车电子,消费电子,工业及网络设备等市 ...
- cache 浅析
http://blog.chinaunix.net/uid-26817832-id-3244916.html 1. Cache Cache一词来源于法语,其原意是"藏匿处,隐秘的地方&q ...
- Scrum介绍
Scrum介绍 摘要 如今,项目管理的步伐越来越快.项目管理需要更灵活.更积极地,向应客户的需求.使用敏捷项目管理方法,项目经理可以在不影响价值.质量和商业规则的前提下实现所有目标,Scrum是一种迭 ...
- linux下查看磁盘空间 [转]
如果要查看磁盘还剩多少空间,当然是用df的命令了. [root@localhost ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/sda2 ...
- php中抓取网页内容的代码
方法一: 使用file_get_contents方法实现 $url = "http://news.sina.com.cn/c/nd/2016-10-23/doc-ifxwztru695114 ...
- vi命令示例大全
1. 进入vi l vi filename: 打开或新建文件,并将光标置于第一行首 l vi +n filename:打开文件,并将光标置于第n行首 l vi + filename:打开文件 ...
- c10k问题及其解决方案
本文主要讲述高并发http应用中的c10k瓶颈问题:在很多服务器初始状态下,无法服务1w左右的并发连接.这与每次服务的资源消耗.服务器的硬件配置固然有关,但很多时候是被linux的默认配置以及软件st ...
- MYSQL校对规则
一.前言 有时候遇到这种情况,你用一个like语句查询,查到的结果中有一些并没有包含你查询的关键词的纪录:有时候遇到这种情况,你的数据库自作聪明的大小写不敏感,让你在更新时把大小写不同的两条记录都更新 ...
- 【NLP】word2vec
http://blog.csdn.net/mytestmy/article/details/26969149?utm_source=tuicool&utm_medium=referral
- jqGrid标题行与第一行之间有很大空白的问题解决。
如题的问题,网上找了很久,都没有解决方案.最后发现,问题不在jqgrid的配置代码,问题在前台HTML代码. <table id="grid" height="30 ...