/**
* 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.
动态规划题目,方法和找最大子串差不多,两个题目共同的特点是
1.每遍历一个元素都有可能影响最后的结果
2.都有局部解和最终解
3.都是通过解决局部解这个小问题而逐渐解决最终解的大问题
做这个题一开始的思路是:回溯法,从第一个数开始走,可能走的步数是[0,nums[i]],遍历可能走的步数,设置一个
变量记录这一步走到哪里了,下一次递归从变量处开始走,发现不能走了之后就返回,这样时间复杂度取决于元素的大小
但是肯定比O(n)大,提交发现超时了
后来发现正确解法是动态规划。局部解就是i + nums[i],全局解就是最大的局部解。每次遍历开始先判断能不能走到这一步
也就是(glo >= i)?,不符合的话直接break,因为如果能到达最后,肯定前边的都能到达。
最后比较glo和nums.length-1的大小。
注意遍历的最终点事nums.length-2,数组的最后一个元素是不遍历的。
*/
public class Q55JumpGame {
public static void main(String[] args) {
int[] nums = new int[]{2,3,1,1,4};
System.out.println(canJump(nums));
}
public static boolean canJump(int[] nums) {
//判断是不是能走到这里
if (nums.length == 1)
return true
int loc;
int glo = 0;
boolean res = false;
for (int i = 0; i < nums.length-1; i++) {
if (glo < i)
break;
//局部解和全局解
loc = i+nums[i];
glo = Math.max(glo,loc);
}
if (glo >= nums.length-1)
res = true;
return res;
}
}

[leetcode]55.JumpGame动态规划题目:跳数游戏的更多相关文章

  1. [leetcode]55. Jump Game青蛙跳(能否跳到终点)

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

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

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

  3. [bzoj1978][BeiJing2010]取数游戏 game_动态规划_质因数分解

    取数游戏 game bzoj-1978 BeiJing-2010 题目大意:给定一个$n$个数的$a$序列,要求取出$k$个数.假设目前取出的数是$a_j$,那么下次取出的$a_k$必须保证:$j&l ...

  4. leetcode 55 Jump Game 三种方法,回溯、动态规划、贪心

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

  5. [SinGuLaRiTy] 动态规划题目复习

    [SinGuLaRiTy-1026] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [UVA 1025] A Spy in the Metr ...

  6. LeetCode初级算法--动态规划01:爬楼梯

    LeetCode初级算法--动态规划01:爬楼梯 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net ...

  7. [LeetCode] 923. 3Sum With Multiplicity 三数之和的多种情况

    Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i &l ...

  8. poj 动态规划题目列表及总结

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  9. 1166 矩阵取数游戏[区间dp+高精度]

    1166 矩阵取数游戏 2007年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description [ ...

随机推荐

  1. rest-framework 响应器(渲染器)

    一 作用: 根据 用户请求URL 或 用户可接受的类型,筛选出合适的 渲染组件. 用户请求URL:    http://127.0.0.1:8000/test/?format=json    http ...

  2. Linux|CentOS下配置Maven环境

    1.下载maven包 wget http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.3.9/binaries/apache-maven ...

  3. 解决 JPA 插入 MySQL 时间与实际时间差 13 个小时问题

    问题描述 公司使用的阿里云数据库服务器,插入时间与实际时间差 13 个小时 执行 show variables like "%time_zone%"; 结果如下: Variable ...

  4. moviepy AudioClip的max_volume方法报错ValueError: operands could not be broadcast together with shapes(2,)

    ☞ ░ 前往老猿Python博文目录 ░ 在<moviepy音视频剪辑:AudioClip的max_volume方法报TypeError: bad operand type for abs(): ...

  5. PyQt(Python+Qt)学习随笔:Designer中ItemViews类部件的frameShadow属性

    老猿Python博文目录 老猿Python博客地址 frameShadow属性是从QFrame继承的属性,对应类型为QFrame.Shadow,该属性表示框架提供三维效果的阴影类型,有如下取值: 可以 ...

  6. linux进程管理(linux命令安装、进程生命周期、进程状态)

    1 linux下如何杀掉进程 1)找到包名所占用的端口: ps aux | grep cbs_portal-1.0.1.jar(包名) 2)杀掉进程: kill 10942(端口号) PS: //-- ...

  7. VirtualBox安装Centos出现E_FAIL (0x80004005)的解决方法

    问题描述:UUID已经存在 Cannot register the hard disk 'F:\hadoop\VirtualBox-centos\centos6.4\centos6.4.vdi' {0 ...

  8. Android10_原理机制系列_事件传递机制

    前言和概述 Android的输入设备,最常用的就是 触摸屏和按键 了.当然还有其他方式,比如游戏手柄,比如支持OTG设备,则可以链接鼠标.键盘等. 那么这些设备的操作 是如何传递到系统 并 控制界面的 ...

  9. pandas 移动列的方法

    import pandas as pd df = pd.DataFrame(np.random.randn(3,4),columns=['a','b','c','d']) k = df.pop(&qu ...

  10. es6 数组新增方法

    1.Array.from(): 这个函数的作用是将类似数组的对象转化为数组,比如DOM对象 let arrayLike = {      "0":"TangSir&quo ...