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 ...
随机推荐
- ASP.NET Web Pages:文件
ylbtech-.Net-ASP.NET Web Pages:文件 1.返回顶部 1. ASP.NET Web Pages - 文件 本章介绍有关使用文本文件的知识. 使用文本文件 在前面的章节中,我 ...
- vs2013编写的ASP.NET网站配置在XP IIS5.1上
1.vs创建项目时,选择.Net Framework2.0 2.配置网站属性 Step1.进入命令行,注册ASP.NET IIS cmd => cd "C:\WINDOWS\Mi ...
- HTC Vive开之unity3d开发
常用的几款插件 Steam VR, SteamVR Unity Toolkit 配置要求:显卡不低于GTX960性能的主机 一.引入手柄交互 1.通过Asset Store导入SteamVR Plu ...
- spring-mvc注解配置小记
Controller中注解Service时,Service的实现类需加@Service,dao的实现类需加@Repository. 另:配置文件中对应的包也需要扫描到!!! <context:a ...
- Tensorflow图像操作
图像操作 图像基本概念 在图像数字化表示当中,分为黑白和彩色两种.在数字化表示图片的时候,有三个因素.分别是图片的长.图片的宽.图片的颜色通道数.那么黑白图片的颜色通道数为1,它只需要一个数字就可以表 ...
- MYSQL体系结构-来自期刊
MySQL三层体系结构 |-----------------------------------------------------------------------------------| | ...
- PHP获取照片exif信息
在这个没图说个屁的年代,照片还是很重要的.如果照片上传后会自动加上 照片相关信息,那用户体验的确会好很多,本着这个想法,今天尝试了下 PHP获取照片exif信息,重要的是 获取图片的GPS信息,如果再 ...
- ASP.NET CMS: Administration Template
ASP.NET CMS: Administration Template For many creating advanced ASP.NET website or application admin ...
- Tornado 中 PyMongo Motor MongoEngine 的性能测试
最近在使用 Tornado 开发 API,数据库选择了 MongoDB,因为想使用 Geo 搜索的特性.Python 可供选择的 MongoDB Drivers 可以在官网查找. 在这些 Driver ...
- 50. linux下查看tomcat日志
cd tomcat/logs/ tail -f catalina.out