这种动规的方法时间复杂度是0(n²)

class Solution {
public:bool canJump(vector<int>& nums) {int length = nums.size();bool can[length];for(int i = 1;i < length;i++)can[i] = false;can[0] = true;for(int i = 1;i < length;i++){for(int j = i-1;j >= 0;j--){if(can[j] == true && nums[j] >= i-j){can[i] = true;break;}}}return can[length-1];}
};

优化:记录能到达的最远距离,时间复杂度只有o(n)

class Solution {
public:bool canJump(vector<int>& nums) {int length = nums.size();if(length < 0)return false;int canarrive = 0;for(int i = 0;i <= canarrive && canarrive < length-1;i++){if(i + nums[i] > canarrive)canarrive = i + nums[i];}return canarrive >= length-1;}
};

转载于:https://www.cnblogs.com/ymjyqsx/p/7468001.html

55.Jump Game相关推荐

  1. 【贪心 和 DP】LeetCode 55. Jump Game

    LeetCode 55. Jump Game 貌似一直就没做几个贪心的题,是时候好好学习一个了.. 参考网址:http://www.cnblogs.com/grandyang/p/4371526.ht ...

  2. 【LeetCode】(55)Jump Game(Medium)

    题目 Jump Game Total Accepted: 52275 Total Submissions: 194395My Submissions Question  Solution  Given ...

  3. 55. Jump Game

    description: 看是否能跳到最后. Note: Example: Example 1:Input: [2,3,1,1,4] Output: true Explanation: Jump 1 ...

  4. 【贪心】LeetCode 55. Jump Game

    LeetCode 45. Jump Game II 参考网址:http://www.cnblogs.com/grandyang/p/4373533.html Solution1: 贪心算法,这里贪婪并 ...

  5. 55. Jump Game 解题记录

    题目描述: Given an array of non-negative integers, you are initially positioned at the first index of th ...

  6. LeetCode 55. Jump Game

    题目描述 You are given an integer array nums. You are initially positioned at the array's first index, a ...

  7. leetcode 55. Jump Game | 55. 跳跃游戏(暴力递归->傻缓存->DP)

    题目 https://leetcode.com/problems/jump-game/ 题解 又是经典套路,暴力递归->傻缓存->DP 没写草稿,直接看代码吧 class Solution ...

  8. 55. Jump Game(跳跃游戏)

    题目链接:https://leetcode.com/problems/jump-game/ 此题的思路同Leetcode45,jumpGame2. Leetcode 45 就是用一个值来保存最大右边界 ...

  9. 55. Jump Game蹦蹦游戏Python Java

    给定一个非负整数数组,您最初位于该数组的第一个索引处.数组中的每个元素代表该位置的最大跳转长度.确定您是否能够达到最后一个索引.类似于第45题 Input: nums = [2,3,1,1,4] Ou ...

最新文章

  1. LAMBDA表达式常用 (全)
  2. favicon支持的图片格式
  3. Enable trace in Resale Order application
  4. expressjs路由和Nodejs服务器端发送REST请求 - - ITeye博客
  5. python实现文件转数组
  6. 使用若依前后端分离下载需要授权的url文件
  7. SpringBoot注册组件之@Configuration@Bean注解作用及注意点
  8. 计算机功能自定义,电脑鼠标自定义按键设置方法
  9. 【C#】打印机ZPL指令,打印文本,中文,条码,图片
  10. CentOS7-Docker-DockerFile-05
  11. .NET开发邮件发送功能的全面教程(含邮件组件源码)
  12. 强网杯2019(高明的黑客强网先锋上单)
  13. stl文件用proe怎么打开_3dMax怎么导入proe的文件渲染?3dmax导入stl文件过程
  14. 大数据的主要学习内容有哪些?
  15. 项目管理之 项目阶段划分
  16. (Java)类的接口实验
  17. OpenCV C++录制视频录制与播放
  18. 计算机应用2010的答案,《计算机应用基础(Windows 7+Office 2010)》参考答案
  19. 联想e550笔记本怎么样_综合性能实测 全文总结_ThinkPad E550_笔记本评测-中关村在线...
  20. 数据结构(一)——链表与邻接表、栈与队列、KMP

热门文章

  1. 修改表格字体颜色_CAD表格文字样式失灵?不,你错了
  2. Android异步下载网络图片(其二:AsyncTask)
  3. CSS3中的transform变形
  4. Tabhost嵌套以及Tab中多个Activity跳转的实现
  5. 使用Python实现Hadoop MapReduce程序
  6. golang中的TestMain
  7. mysql表级锁和行级锁
  8. thinkphp-add方法错误
  9. linux中yum命令详解
  10. CentOS下安装protobuf