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.

思路:在i位置如果能到last index,那么能到达=>局部最优解就是全局最优解=>贪心法。每次求i位置能到达的最远距离。

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

55. Jump Game (Array; Greedy)的更多相关文章

  1. 55. Jump Game leetcode

    55. Jump Game Total Accepted: 95819 Total Submissions: 330538 Difficulty: Medium Given an array of n ...

  2. [Leetcode][Python]55: Jump Game

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ...

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

  4. leetcode 55. Jump Game、45. Jump Game II(贪心)

    55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...

  5. 刷题55. Jump Game

    一.题目说明 题目55. Jump Game,给定一组非负数,从第1个元素起,nums[i]表示你当前可以跳跃的最大值,计算能否到达最后一个index.难度是Medium. 二.我的解答 非常惭愧,这 ...

  6. [leetcode greedy]55. Jump Game

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

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

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

  8. [LeetCode] 55. Jump Game 解题思路

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

  9. 55 Jump Game i && 45 Jump Game ii

    Jump Game Problem statement: Given an array of non-negative integers, you are initially positioned a ...

随机推荐

  1. charles只获取指定的请求的设置方法

    过滤网络请求 通常情况下,需要对网络请求进行过滤,只监控指定服务器的请求.有3种办法: 方法一:在主界面的中部的 Filter 栏中输入需要过滤出来的关键字.例如我们的服务器的地址(host)是:ww ...

  2. appium+python自动化43-微信公众号webview操作

    前言 上一篇已经解决切换到微信公众号的webview上了,但是定位webview上元素的时候一直提示找不到,打印page_source也找不到页面上的元素,这个问题困扰了一整天,还好最后找到了原因, ...

  3. 转载CopyOnWriteArrayList

    转载原文 http://www.cnblogs.com/dolphin0520/p/3938914.html Copy-On-Write简称COW,是一种用于程序设计中的优化策略.其基本思路是,从一开 ...

  4. Eclipse里面的Maven项目如果下载依赖的jar包的源码

    Window---------Properties---------------Maven--------------勾选Download Artifact Sources和Download Arti ...

  5. php array_flip() 删除数组重复元素——大彻大悟

    1. php array_flip() 删除数组重复元素,如果用于一维索引数组,好理解. [root@BG-DB:~]$more arr.php  <?php         $arr = ar ...

  6. JAVA中关于set()和get()方法的理解及使用

    对于JAVA初学者来说,set和get这两个方法似乎已经很熟悉了,这两个方法是JAVA变成中的基本用法,也是出现频率相当高的两个方法. 为了让JAVA初学者能更好的理解这两个方法的使用和意义,今天笔者 ...

  7. 小朋友学C++(1)

    Hello World! 在学C++之前,最好先学习一下C语言 让我们先运行一段简单的代码,编译器可以使用 在线C++编译器 或 Xcode(苹果系统) 或Dev C++(Windows系统). #i ...

  8. 一种思路,隐藏input标签,通过label关联

    <label class="btn btn-default btn-file">上传图片 <input hidden type="file" ...

  9. 24. Swap Nodes in Pairs + 25. Reverse Nodes in k-Group

    ▶ 问题:单链表中的元素进行交换或轮换. ▶ 24. 每两个元素进行翻转.如 [1 → 2 → 3 → 4 → 5] 变换为 [2 → 1 → 4 → 3 → 5] ● 初版代码,4 ms class ...

  10. leetcode108

    /** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...