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.

Example 1:

Input: [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Solution 1:
DP
class Solution {
public boolean canJump(int[] nums) {
if (nums == null || nums.length <= 1) {
return true;
}
boolean[] arr = new boolean[nums.length];
arr[0] = true;
for (int i = 1; i < nums.length; i++) {
for (int j = 0; j < i; j++) {
if (arr[j] && (j + nums[j] >= i)) {
arr[i] = true;
break;
}
}
}
return arr[nums.length - 1];
}
}

Solution 2:
Greedy
class Solution {
public boolean canJump(int[] nums) {
int max = 0;
for (int i = 0; i < nums.length; i++) {
if (i > max) {
return false;
}
max = Math.max(max, i + nums[i]);
}
return true;
}
}

[LC] 55. Jump Game的更多相关文章

  1. 55. Jump Game leetcode

    55. Jump Game Total Accepted: 95819 Total Submissions: 330538 Difficulty: Medium Given an array of n ...

  2. [Leetcode][Python]55: Jump Game

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ...

  3. Leetcode 55. Jump Game & 45. Jump Game II

    55. Jump Game Description Given an array of non-negative integers, you are initially positioned at t ...

  4. leetcode 55. Jump Game、45. Jump Game II(贪心)

    55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...

  5. 刷题55. Jump Game

    一.题目说明 题目55. Jump Game,给定一组非负数,从第1个元素起,nums[i]表示你当前可以跳跃的最大值,计算能否到达最后一个index.难度是Medium. 二.我的解答 非常惭愧,这 ...

  6. [LeetCode] 55. Jump Game 跳跃游戏

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  7. [LC] 45. Jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  8. Leetcode 55. Jump Game

    我一开始认为这是一道DP的题目.其实,可以维护一个maxReach,并对每个元素更新这个maxReach = max(maxReach, i + nums[i]).注意如果 i>maxReach ...

  9. Jump Game 的三种思路 - leetcode 55. Jump Game

    Jump Game 是一道有意思的题目.题意很简单,给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置. 比如: A = ...

随机推荐

  1. LNMP安装问题

    查什么占用了端口   netstat -nlp |grep :80 root@zzx:/usr/local/mysql# netstat -nlp |grep :80tcp        0      ...

  2. 网页时不时打不开?试试阿里DNS 233.5.5.5 /233.6..6.6

    最经上网都是用手机热点,但发现用谷歌浏览器时,时不时打不开网页.最后发现是DNS的问题,原来我的dns是8.8.8.8. 最后更改成阿里的DNS 233.5.5.5 /233.6..6.6,打开网页流 ...

  3. 关于linux下安装mysqlclient报 Failed building wheel for mysqlclient问题

    导入下列依赖包,搞定 sudo apt-get install python3 python-dev python3-dev build-essential libssl-dev libffi-dev ...

  4. Spring framework体系架构

    Spring3.x 图中将spring分为5个部分:core.aop.data access.web.test,图中每个圆角矩形都对应一个jar,如果在maven中配置,所有这些jar的"g ...

  5. 12 Spring Data JPA:orm思想和hibernate以及jpa的概述和jpa的基本操作

    spring data jpa day1:orm思想和hibernate以及jpa的概述和jpa的基本操作 day2:springdatajpa的运行原理以及基本操作 day3:多表操作,复杂查询 d ...

  6. Linux学习(二) --- 常用命令

    [TOC] 一.常用命令 1.目录 cd切换 cd 路径 切换到指定路径 cd .. 上一级 mkdir:创建目录 mkdir 目录名 pwd:查看 2.查看 ll命令:查询目录内容 ll 查看当前( ...

  7. JavaSE--【JAVA】unicode为12288字符

    转载:http://blog.csdn.net/zfpigpig/article/details/8186470 今天做一个导入数据功能时发现一个问题,就是一个unicode为12288的字符显示为空 ...

  8. 异常处理和UDP协议

    一.什么是异常? 程序在运行过程中出现了不可预知的错误,并且该错误没对应的处理机制,那么就会以异常的形式表示出来, 造成的影响就是整个程序无法再正常的运行,抛出异常. 二.异常的结构: 1:异常的类型 ...

  9. [GX/GZOI2019]与或和(单调栈+按位运算)

    首先看到与或,很显然想到按照位拆分运算.然后就变成了0/1矩阵,要使矩阵在当前位与为1,则矩阵全为1,如果是或为1,则是矩阵不全为0,然后求全为0/1的矩阵个数即可.记录c[i][j]表示以a[i][ ...

  10. [SDOI2019]热闹又尴尬的聚会(图论+set+构造)

    据说原数据可以让复杂度不满的暴力O(Tn^2)过掉……O(Tn^2)方法类似于codeforces一场div2的E题 有一种比较好的方法:每次找出原图G中度最小的点加入q,然后将相邻的点加入新图G'. ...