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 ...
随机推荐
- puppy-language
puppy language是一种解释型的结构化脚本语言. 项目地址: https://github.com/zlvb/PuppyLanguage 因为一直很忙,有段时间没去更新了,不过功能已经比较完 ...
- C++ 记事本: 变量
C++ 变量也许和其他语言的变量没有什么差别.就是用来存储一些可能会变值的容器. 当然 C++ 变量里又分为 原子类型 的(int , char ,bool 等等),复合类型 的(struct ,cl ...
- Windows 系统下设置Nodejs NPM全局路径
Windows下的Nodejs npm路径是appdata,很不爽,想改回来,但是在cmd下执行以下命令也无效 npm config set cache "D:\nodejs\node_ca ...
- Scala 并行和并发编程-Futures 和 Promises【翻译】
官网地址 本文内容 简介 Futures 阻塞 异常 Promises 工具 最近看了<七周七语言:理解多种编程泛型>,介绍了七种语言(四种编程范型)的主要特性:基本语法,集合,并行/并发 ...
- 代码生成器Sql Server 和 Mysql 数据库脚本
经常用到代码生成器,对于取数据脚本做个记录: #region SQL-SqlServer private string SqlTableList = @"SELECT so.name, Co ...
- 连上VPN后,如何访问内网(添加路由表实现网络分流方法)
route add 192.168.4.0 mask 255.255.255.0 192.168.2.0 metric 1 不止有“邮件系统”会出现这种情况,还有其他情况,这时,你需要在没有连接外网和 ...
- ABAP报表中负值展示问题的处理方法
现象描述 在使用ABAP报表展示数据的时候会涉及到金额类字段,在手动计算金额的时候,有时会发生存在负值而无法正常展示的情况. 处理过程 ABAP报表的数据展示常用的方法有两种,分别是表控制和ALV ...
- Windows调试学习笔记:(二)WinDBG调试.NET程序示例
好不容易把环境打好了,一定要试试牛刀.我创建了一个极其简单的程序(如下).让我们期待会有好的结果吧,阿门! using System; using System.Collections.Generic ...
- System.Net.WebException : The remote server returned an error: (415) UNSUPPORTED MEDIA TYPE
I am having problems with a bit of code that accesses a restful web service. Running this code, it e ...
- [Core Javascirpt] Basic Metaprogramming: Dynamic Method
Somehow it looks like reflect in Java. For example: We define an mothod on the Object, it called def ...