/**
* 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. shiro利用过期时间,解决用户冻结踢出问题

    背景 shiro中需要冻结某个用户,但是此时此刻这个用户在线,如果冻结只是改变状态的话,只会导致用户不满,所以要改变这个办法. 在查找过程中发现都是告诉shiro写自定义过滤器,那么我如果自定义过滤器 ...

  2. 网络编程原理与UDP实现

    如何发送数据包? Q:当应用程序产生数据的时候,需要去构造数据包并发送到网络上去,但是由谁负责处理呢? A:现代操作系统负责数据包得构造与发送,应用程序只需提供数据. 当应用程序产生数据时,应用程序将 ...

  3. 关于老猿Python系列文章发布网址变化的说明

    老猿Python系列文章最开始在新浪发布,后逐渐开通了CSDN.博客园和简书三个网址,但老猿一来工作忙,二来Python需要学习的内容太多,因此实在没时间同时维护这么多博客,事实上除了CSDN其他网站 ...

  4. 问题:PyCharm的几种调试方法的区别

    关于PyCharm的调试方式,step into.step over.step out.run to cursor.resume programe与c语言相关的调试器功能基本相同,但PyCharm提供 ...

  5. web网络漏洞扫描器编写

    这两天看了很多web漏洞扫描器编写的文章,比如W12scan以及其前身W8scan,还有猪猪侠的自动化攻击背景下的过去.现在与未来,以及网上很多优秀的扫描器和博客,除了之前写了一部分的静湖ABC段扫描 ...

  6. ripple Failed to load resource: the server responded with a status of 404 (Not Found)

    在VS2015中使用Cordova + typescript开发中,遇到个问题. 在javascript console 中提示: Failed to load resource: the serve ...

  7. 团队作业4-Day6

    团队作业4-Day6 项目git地址 1. 站立式会议 2. 项目燃尽图 3. 适当的项目截图 4. 代码/文档签入记录(部分) 5. 每人每日总结 吴梓华:今日修复了图片显示BUG,补充了排位模式出 ...

  8. Panda交易所视点观察:政府连发区块链建设文件,相关概念股受追捧

    日前,Panda交易所从北京市地方金融监督管理局获悉,证监会已同意在北京.苏州.上海.浙江.深圳等地区的区域性股权市场参与区块链建设工作.以上5市金融监管局将按照中国证监会的统一部署要求推进建设工作. ...

  9. SnowFlakeldWorker

    SnowFlakeldWorker java /** * Twitter_Snowflake * SnowFlake的结构如下(每部分用-分开): * 0 - 0000000000 000000000 ...

  10. C++异常之五 异常和继承

    异常和继承 异常也是类,我们可以创建自己的异常类,在异常中可以使用(虚函数,派生,引用传递和数据成员等), 下面用一个自制的数组容器Vector,在对Vector初始化时来对Vector的元素个数进行 ...