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 ...
随机推荐
- CodeForces 706D Vasiliy's Multiset
字典树. 比较经典的题目了.把每一个数字都插入到字典树中,询问的时候如果$x$的第$i$位是$p$,那么尝试着在字典树上往$pXOR1$的节点走下去,没有$pXOR1$节点的话再走$p$的.删除操作的 ...
- JavaScript忍者秘籍——闭包
概要:本篇博客主要介绍了JavaScript的闭包 1.闭包的工作原理 简单地说,闭包就是一个函数在创建时允许该自身函数访问并操作该自身函数之外的变量时所创建的作用域. 例如: var outerVa ...
- 【Python】使用super初始化超类
初始化超类的传统方式,在子类的实例中调用超类的__init__()方法. 但是传统的方法有两个问题,比如: 问题1: class MyBaseClass: def __init__(self, val ...
- [Q]无法卸载怎么办
正确卸载CAD批量打图精灵的方法是进入操作系统,“控制面版”,然后运行“添加和删除程序”,找到CAD批量打图精灵,选“更改/删除”,按照提示操作,即可进行卸载. 若使用强制卸载工具(如360等)卸载可 ...
- php 便利数组方法
数组在PHP中是一个非常强大的武器,用起来方便.容易,由于使用起来异常灵活,用它就可以实现数据结构中的链表.栈.队列.堆以及所谓的字典.集合等,也可以转换成XML格式. 1.使用for for语句遍历 ...
- WPF 命令的简单总结
WPF的命令Command主要解决的问题,就是代码复用.一个很重要的应用意义,在于它将很多地方需要的调用的相同操作,以统一的方式管理,却又提供了不同的访问结果. 举个例子来说,我可能通过“点击butt ...
- C++友元
通过friend关键字,我们可以将不属于当前类的一个函数在当前类中加以声明,该函数便可以成为当前类的友元函数. 例1: #include<iostream>using namespace ...
- Java版经典兔子繁殖迭代问题——斐波那契(Fibonacci)数列
/** * 题目: * 有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子. * 假如兔子都不死,问经过month个月后,兔子的总数为多少对? */ public ...
- 使用SyncToy 同步两台机器上的文件夹
@echo off echo 准备启动同步... net use \\WIN-AJH8QENQQGK "123456" /user:Administrator Z:\SyncToy ...
- 用 yo aspnet 生成.net项目
yo指的是Yeoman 官网:http://yeoman.io/ 因为安装yo需要nmp 因此 要先到node官网下载node并按装 安装之后就可以下一步了 $ npm install -g yo g ...