[LC] 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.
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的更多相关文章
- 55. Jump Game   leetcode
		
55. Jump Game Total Accepted: 95819 Total Submissions: 330538 Difficulty: Medium Given an array of n ...
 - [Leetcode][Python]55: Jump Game
		
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ...
 - 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 ...
 - leetcode 55. Jump Game、45. Jump Game II(贪心)
		
55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...
 - 刷题55. Jump Game
		
一.题目说明 题目55. Jump Game,给定一组非负数,从第1个元素起,nums[i]表示你当前可以跳跃的最大值,计算能否到达最后一个index.难度是Medium. 二.我的解答 非常惭愧,这 ...
 - [LeetCode] 55. Jump Game 跳跃游戏
		
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
 - [LC] 45. Jump Game II
		
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
 - Leetcode 55. Jump Game
		
我一开始认为这是一道DP的题目.其实,可以维护一个maxReach,并对每个元素更新这个maxReach = max(maxReach, i + nums[i]).注意如果 i>maxReach ...
 - Jump Game 的三种思路 - leetcode 55. Jump Game
		
Jump Game 是一道有意思的题目.题意很简单,给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置. 比如: A = ...
 
随机推荐
- Cisco连接失败问题处理
			
连接公司的VPN时软件一直安装不上,试了几种方法,在此总结. 原文链接:http://www.itsystemadmin.com/error-27850-unable-to-manage-networ ...
 - 美团:WSDM Cup 2019自然语言推理任务获奖解题思路
			
WSDM(Web Search and Data Mining,读音为Wisdom)是业界公认的高质量学术会议,注重前沿技术在工业界的落地应用,与SIGIR一起被称为信息检索领域的Top2. 刚刚在墨 ...
 - Python笔记_第四篇_高阶编程_正则表达式_3.正则表达式深入
			
1. re.split 正则的字符串切割 str1 = "Thomas is a good man" print(re.split(r" +",str1)) # ...
 - java基础-泛型的优点
			
1.性能 对值类型使用非泛型集合类,在把值类型转换为引用类型,和把引用类型转换为值类型时,需要进行装箱和拆箱操作.装箱和拆箱的操作很容易实现,但是性能损失较大.假如使用泛型,就可以避免装箱和拆箱操作. ...
 - java数目
			
第一部分: Java语言篇1 <Java编程规范>星级:适合对象:初级,中级介绍:作者James Gosling(Java之父),所以这本书我觉得你怎么也得读一下.对基础讲解的很不错. 2 ...
 - bfs--P1443 马的遍历
			
有一个n*m的棋盘(1<n,m<=400),在某个点上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步 跟迷宫一样,找最近距离,显然用bfs,两个方位数组dir1和dir2用来表示 ...
 - 阿里云ECSLinux系统下挂载磁盘
			
最近公司服务器老是提示磁盘空间不足,原因是以前的业务负责人开了Tomcat的debug日志并且没有做日志轮询,所以日志量非常大.当我做了日志切割轮询后发现磁盘还是太小才40G,按理外网服务器怎么可能这 ...
 - vue每次运行起来端口不一致问题
			
原因:portfinder新发布的版本异常 解决方案:npm install portfinder@1.0.21
 - 【@ConfigurationProperties注解】Not Found The requested URL /spring-boot/docs/2.2.2.RELEASE/reference/html/configuration-metadata.html was not found on this server.
			
<!-- 配置文件自动映射 --> <dependency> <groupId>org.springframework.boot</groupId> & ...
 - ACM-ICPC Asia Beijing Regional Contest 2018 Reproduction hihocoder1870~1879
			
ACM-ICPC Asia Beijing Regional Contest 2018 Reproduction hihocoder1870~1879 A 签到,dfs 或者 floyd 都行. #i ...