55. Jump Game (Array; Greedy)
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)的更多相关文章
- 55. Jump Game leetcode
55. Jump Game Total Accepted: 95819 Total Submissions: 330538 Difficulty: Medium Given an array of n ...
- [Leetcode][Python]55: Jump Game
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ...
- 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 ...
- leetcode 55. Jump Game、45. Jump Game II(贪心)
55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...
- 刷题55. Jump Game
一.题目说明 题目55. Jump Game,给定一组非负数,从第1个元素起,nums[i]表示你当前可以跳跃的最大值,计算能否到达最后一个index.难度是Medium. 二.我的解答 非常惭愧,这 ...
- [leetcode greedy]55. Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 55. Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 55. Jump Game 解题思路
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 55 Jump Game i && 45 Jump Game ii
Jump Game Problem statement: Given an array of non-negative integers, you are initially positioned a ...
随机推荐
- mysql分区表之一:分区原理和优缺点【转】
1.分区表的原理 分区表是由多个相关的底层表实现,这些底层表也是由句柄对象表示,所以我们也可以直接访问各个分区,存储引擎管理分区的各个底层表和管理普通表一样(所有的底层表都必须使用相同的存储引擎),分 ...
- php文件下载(解决文件下载后多几个字节的问题) 与封装成类的例子
php文件下载比较常见,网上的资料比较多,在此不再强调怎么去实现(因为也是网上看的).下面主要说明的是下载代码的注意点. php下载文件主要是把文件以字节流直接输出,也就是echo fread($fi ...
- CentOS所有版本下载地址分享
简述 CentOS(Community Enterprise Operating System - 社区企业操作系统)是Linux发行版之一,它是来自于Red Hat Enterprise Linux ...
- C++多线程同步之事件(Event)
原文链接:http://blog.csdn.net/olansefengye1/article/details/53291074 一.事件(Event)原理解析 1.线程同步Event,主要用于线程间 ...
- WinRAR 代码执行漏洞复现
影响版本: WinRAR < 5.70 Beta 1 Bandizip < = 6.2.0.0 好压(2345压缩) < = 5.9.8.10907 360压缩 & ...
- python面向对象重新梳理
关于本篇博文: 面向对象中所有的内容的重新梳理,其实面向对象的知识早在一个多月前就学习过并整理过,但是发现还是有所欠缺,故在此以极其简介的语言风格重新梳理一遍 面向对象详细介绍:http://www. ...
- 2018-2019-2 《网络对抗技术》Exp3 免杀原理与实践 Week5 20165233
Exp3 免杀原理与实践 实验内容 一.基础问题回答 1.杀软是如何检测出恶意代码的? 基于特征码的检测:通过与自己软件中病毒的特征库比对来检测的. 启发式的软件检测:就是根据些片面特征去推断.通常是 ...
- shell语法(二)
Shell脚本语法 条件测试:test. [ ] 命令test或[可以测试一个条件是否成立,如果测试结果为真,则该命令的Exit Status为0,如果测试结果为假,则命令的Exit Status为1 ...
- DevExpress 数据与展示的不同
有时候我们需要详细展示数据源的含义,这时候就需要用到 RepositoryItemLookUpEdit 展示: 代码详情: ColumnData = new DevExpress.XtraGrid.C ...
- HTML5 Canvas ( 贝塞尔曲线, 一片星空加绿地 ) quadraticCurveTo, bezierCurveTo
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...