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. selenium浏览器内核监测处理

    一.代码 from selenium.webdriver import Chrome from selenium.webdriver import ChromeOptions option = Chr ...

  2. Julia1.x安装

    删除julia-1.0 $ jupyter kernelspec list Available kernels: julia-1.0 /Users/keke.zhaokk/Library/Jupyte ...

  3. Java 工厂模式登陆系统实现

    没有工厂模式 设定一个登陆系统 UserServiceImp.java public class UserServiceImp { public boolean login(String userna ...

  4. quartz定时定时任务执行两次

    quartz框架没问题. 流程: sping-quartz配置 <?xml version="1.0" encoding="UTF-8"?> < ...

  5. centos快速安装mysql

    1. wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm 2. rpm -ivh mysql-community-r ...

  6. redis day03 下

    事务 能够有回退状态 事务命令 安命令执行没问题,redis是弱事务型 nulti incr n1   -->QUEUED(返回仅队列了) EXEC -->返回结果 pipeline 流水 ...

  7. Xshell便捷设置实现linux复制粘贴

    说明:在window系统中,Ctrl+C是复制的快捷键,Ctrl+V是粘贴的快捷键,但在xshell中,Ctrl+C 代表着中断当前指令. 解决办法如下: 1.  使用xshell中的复制粘贴快捷键复 ...

  8. 怎么通过scanf读取一个空白前的字符

    /************************************************************************* > File Name: scanf2.c ...

  9. ios键盘退出

    点击空白处退出键盘 -(void)touchesBegan:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent ...

  10. stopping service [tomcat],服务未启动

    1. 在主类中添加日志打印,查看错误: 2. 我的错误是因为两个controller含有相同的路径