55. Jump Game

第一种方法:

只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break

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

第二种方法:

第一种方法时间复杂度高且需要O(n)的空间复杂度。这题用贪心在O(n)的时间复杂度,O(1)的空间复杂度就可以解决。

用一个reach变量记录当前位置能达到的最远的位置索引,每次更新,如果reach比当前的索引小伙子reach已经能到达最后一个位置就可以break掉。

注意:reach及其比较的对象都是数组的索引,即n-1。

https://www.cnblogs.com/grandyang/p/4371526.html

class Solution {
public:
bool canJump(vector<int>& nums) {
if(nums.empty())
return false;
int reach = ;
for(int i = ;i < nums.size();i++){
if(reach < i || reach >= nums.size() - )
break;
reach = max(reach,i + nums[i]);
}
return reach >= nums.size() - ;
}
};

45. Jump Game II

https://www.cnblogs.com/grandyang/p/4373533.html

pre记录的是前一次能达到的最远位置的索引,cur是当前能达到的最远位置的索引。

你每一次的更新其实都是在前一次最远距离的范围内更新下一次的最远距离。

这个i是当前遍历的位置。

class Solution {
public:
int jump(vector<int>& nums) {
int cur = ,res = ,i = ;
int pre;
while(cur < nums.size() - ){
res++;
pre = cur;
for(;i <= pre;i++)
cur = max(cur,i + nums[i]);
}
return res;
}
};

leetcode 55. Jump Game、45. Jump Game II(贪心)的更多相关文章

  1. 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 ...

  2. 力扣Leetcode 45. 跳跃游戏 II - 贪心思想

    这题是 55.跳跃游戏的升级版 力扣Leetcode 55. 跳跃游戏 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃 ...

  3. LeetCode 55. 跳跃游戏(Jump Game)

    题目描述 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: ...

  4. 贪心——55. 跳跃游戏 && 45.跳跃游戏II

    给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: true ...

  5. [Leetcode][Python]45: Jump Game II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...

  6. Leetcode 45. Jump Game II(贪心)

    45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...

  7. [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)

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

  8. [LeetCode] 45. Jump Game II 跳跃游戏 II

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

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

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

随机推荐

  1. linux内核构造skb发包-----raw、tcp网络编程

    1. 内核raw发包 #include <linux/init.h>#include <linux/module.h> #include <linux/kernel.h& ...

  2. 【python cookbook】找出序列中出现次数最多的元素

    问题 <Python Cookbook>中有这么一个问题,给定一个序列,找出该序列出现次数最多的元素.例如: words = [ 'look', 'into', 'my', 'eyes', ...

  3. Vim使用技巧(0) -- 博主的vim配置

    vim ~/.vimrc "插入模式时 光标的上下左右移动 inoremap <C-l> <Right> inoremap <C-h> <Left& ...

  4. EntityFramework 事物引发的问题

    前记 还是最近做的日志模块,今天做最后的入库工作.在测试入库日志记录时,总是出现怪异的问题. 开启服务开始接收 Kafka 的消息,第一条数据没有问题,后面的都如不了库.很是懵~~~ 调试了很久定位在 ...

  5. javaScript基础及初始面向对象

    对象是什么?对象是包含相关属性和方法的集合体属性方法什么是面向对象面向对象仅仅是一个概念或者编程思想通过一种叫做原型的方式来实现面向对象编程 创建对象自定义对象内置对象 自定义对象2-1基于Objec ...

  6. 定时器 延时调用setTimeout

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 使用jQuery快速高效制作网页交互特效-----JavaScript操作DOM对象

    一.DOM操作分类 使用JavaScript操作DOM时通常分为三类:DOM    Core.HTMl--DOM和CSS-DOM 二.访问节点 节点属性 三.节点信息 四.操作节点的属性 语法: ge ...

  8. PHP读取文件内容的方法

    下面我们就为大家详细介绍PHP读取文件内容的两种方法. 第一种方法:fread函数 <?php $file=fopen('1.txt','rb+'); echo fread($file,file ...

  9. 如何利用Wireshark解密SSL和TLS流量

    如何利用Wireshark解密SSL和TLS流量https://support.citrix.com/article/CTX135121 1.有server端的private key,直接在wires ...

  10. 5-STM32物联网开发WIFI(ESP8266)+GPRS(Air202)系统方案升级篇(,远程升级GPRS内部程序)

    https://www.cnblogs.com/yangfengwu/p/10410202.html 与升级WIFI相同介绍的不再叙述  先看WIFI升级的: ↑ 演示视频: https://www. ...