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 ...
随机推荐
- js获取数组长度,对象成员个数字、符串字数
文章来源:百度文库 Javascript怎么得到数组长度(也就是数组的元素个数)? Javascript怎么获取对象的成员个数? 你肯定想到了array.length!? 那么我们来测试一下下面这 ...
- [UE4]为什么会有类型检查
类型检查 定义:对象的行为是否符合类型的行为 作用:帮助开发者找出潜在的错误. 类型转换 隐式类型转换:整数可以和浮点数运算 显式类型转换/强制类型转换
- [UE4]C 语言动态数组
在实际的编程中,往往会发生这种情况,即所需的内存空间取决于实际输入的数据,而无法预先确定.对于这种问题,用静态数组的办法很难解决.为了解决上述问题,C语言提供了一些内存管理函数,这些内存管理函数结合指 ...
- jQuery 的noConflict()的使用.
我们项目现在需要用到两个js库.一个是jQuery库,还有一个是我们自己开发的轻量级的gys.js库. 而gys库对外提供的接口也是$符号.和jQuery库是一样的,这个时候,两个库就会发生冲突了,我 ...
- 【Unix网络编程】chapter3 套接字编程简介
chapter3套接字编程简介3.1 概述 地址转换函数在地址的文本表达和他们存放在套接字地址结构中的二进制值之间进行转换.多数现存的IPv4代码使用inet_addr和inet_ntoa这两个函数, ...
- pip安装包(python安装gevent(win))
下载: https://www.lfd.uci.edu/~gohlke/pythonlibs/#greenlet greenlet greenlet-0.4.14-cp36-cp36m-win_amd ...
- solr之~模糊查询
有的时候,我们一开始不可能准确地知道搜索的关键字在 Solr 中查询出的结果是什么,因此,Solr 还提供了几种类型的模糊查询.模糊匹配会在索引中对关键字进行非精确匹配.例如,有的人可能想要搜索某个前 ...
- Kafka 基本原理
Kafka 基本原理 来源:阿凡卢 , www.cnblogs.com/luxiaoxun/p/5492646.html 简介 Apache Kafka是分布式发布-订阅消息系统.它最初由Link ...
- git常用命令,冲突
使用多个仓库git push cangkuming fenzhiming删除远程仓库 git push 远程仓库名 :删除目标分支 # 需要先删除本地目标分支 git pull <远程主机名&g ...
- png-CRC32校验
官方文档: https://www.w3.org/TR/PNG-CRCAppendix.html CRC32校验的数据,看原文 A four-byte CRC (Cyclic Redundancy C ...