/**
* 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. ios真机使用fixed定位页面滚动时fixed定位的元素也会跟着滚动

    到了ios真机APP中,页面向下滚动,fixed的元素也跟着滚,虽然最后它还是到了它该在的地方,但是它跟着滚动也很影响页面的流畅性和交互性好伐.

  2. 三万字无坑搭建基于Docker+K8S+GitLab/SVN+Jenkins+Harbor持续集成交付环境

    写在前面 最近在 K8S 1.18.2 版本的集群上搭建DevOps环境,期间遇到了各种坑.目前,搭建环境的过程中出现的各种坑均已被填平,特此记录,并分享给大家! 文章和搭建环境所需要的yml文件已收 ...

  3. springmvc跨域问题

    1.跨域问题: 按照网上所有的方法试了一遍,都没跨过去,正在无助之际,使用filter按照下面的方法解决的时候出现了转机: 添加filter: package com.thc.bpm.filter; ...

  4. PHP代码审计分段讲解(2)

    03 多重加密 源代码为: <?php include 'common.php'; $requset = array_merge($_GET, $_POST, $_SESSION, $_COOK ...

  5. this作为构造函数时注意点

    在 JS 中,为了实现类,我们需要定义一些构造函数,在调用一个构造函数的时候加上 new 这个关键字: function Person(name) { this.name = name; consol ...

  6. ARM架构安装Kubernetes集群

    背景 类型 版本 操作系统 CentOS Linux release 7.6.1810 (AltArch) 内核 Linux master 4.18.0-80.7.2.el7.aarch64 硬件配置 ...

  7. java性能问题排查提效脚本工具

    在性能测试过程中,往往会出现各种各样的性能瓶颈.其中java常见瓶颈故障模型有cpu资源瓶颈:文件IO瓶颈:网络IO瓶颈:内存资源瓶颈:资源消耗不高程序本身执行慢等场景模型. 如何快速定位分析这些类型 ...

  8. 利用神经网络算法的C#手写数字识别(一)

    利用神经网络算法的C#手写数字识别 转发来自云加社区,用于学习机器学习与神经网络 欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 下载Demo - 2.77 MB (原始地址):handwri ...

  9. nginx优化-转载

    (1)nginx运行工作进程个数,一般设置cpu的核心或者核心数x2 如果不了解cpu的核数,可以top命令之后按1看出来,也可以查看/proc/cpuinfo文件 grep ^processor / ...

  10. Nacos源码深度解析1-服务注册初始化(客户端)

    一.初始化 NamingService naming = NamingFactory.createNamingService(properties); 二.通过反射传入properties生成Naco ...