LeetCode OJ 55. Jump Game
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
.
【题目分析】
A[i]的值代表如果到达 i 时可以向前跳的最大步长,判断从i = 0出发是否能到达数组的最后一个值。
【思路】
用distance记录当前i之前跳的最远距离。如果distance< i,即代表即使再怎么跳跃也不能到达i。当到达i时,A[i]+i,代表从i能跳最远的距离。max(distance,A[i]+i)代表能到达的最远距离。
【java代码】
public class Solution {
public boolean canJump(int[] nums) {
if(nums == null || nums.length == 0) return true; int distance = 0;
for(int i = 0; i < nums.length && i <= distance; i++){
distance = Math.max(nums[i]+i, distance);
}
return distance < nums.length -1 ? false : true;
}
}
LeetCode OJ 55. Jump Game的更多相关文章
- [Leetcode][Python]55: Jump Game
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ...
- 【一天一道LeetCode】#55. Jump Game
一天一道LeetCode系列 (一)题目 Given an array of non-negative integers, you are initially positioned at the fi ...
- 【LeetCode】55. Jump Game 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcod ...
- LeetCode OJ 45. Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [leetcode greedy]55. Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【LeetCode】55. Jump Game
Jump Game Given an array of non-negative integers, you are initially positioned at the first index o ...
- LeetCode OJ:Jump Game II(跳跃游戏2)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode OJ:Jump Game(跳跃游戏)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 55. Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- LED的串联电阻值的计算
与LED串联的电阻被用于控制该LED导通时的电流量.为了计算电阻值,你需要知道输入电源电压(Vs,一般为5V),LED的正向电压(Vf)和你需要流过LED的电源(/)的数值. 其电阻欧姆值的计算公式( ...
- Linux的一些简单命令(二)
1.查看防火墙状态:service iptables status 2.开启防火墙:service iptables start 3.关闭防火墙:service iptables stop 4.创建目 ...
- javascriptDOM对象之scrollTo()方法,滚动到页面指定位置
scrollTo是一个神奇的方法,常用于篇幅过长的页面,制作一个回顶部的按钮,我这里简单的实现以下 当然没有一个过渡的js效果 scrollTo(xpos,ypos) 参数 描述 xpos 必需.要在 ...
- CentOS7 安装 OpenSSL 1.0.1m 和 OpenSSH 6.8p1
# 下载软件 wget http://zlib.net/zlib-1.2.8.tar.gz wget ftp://ftp.openssl.org/source/openssl-1.0.1m.tar.g ...
- KMP算法 学习例题 POJ 3461Oulipo
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37971 Accepted: 15286 Description The ...
- vb.net 结束进程
Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ...
- JavaScript忍者秘籍——原型
概要:本篇博客主要介绍JavaScript的原型 1.对象实例化 - 初始化的优先级 初始化操作的优先级如下: ● 通过原型给对象实例添加的属性 ● 在构造器函数内给对象实例添加的属性 在构造器内的绑 ...
- 安装配置Postgresql
//关闭selinuxgetenforcesetenfroce 0vi /etc/selinux/configSELINUX=disabledSELINUXTYPE=targeted//关闭防火墙#c ...
- C# 导出Word
导出word文档(无模版): using Microsoft.Office.Interop.Word; public static string CreateWordFile(CaseVM model ...
- centos 日常操作指令
ls ls -li 查看当前目录下所有目录 ls -al 查看当前目录下所有目录包括隐藏文件 CD cd 目录 跳转指定目录 cd ..返回上级目录 cd / 返回根目录 VI 1. 使用vi进 ...