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 ...
随机推荐
- Unity3D NGUI Sprite精灵动画
NGUI 2.6.1下载: part1 part2 NGUI 实现Sprite精灵动画很简单: 1.先制作图像集合.打开NGUI菜单下Atlas Maker,选中切好的图片,点击Add/Update按 ...
- airtest IDE问题汇总
FAQ 1.同一个脚本,使用IDE可以运行,使用命令行运行报错 原因:曾经开启过anyproxy代理,添加过HTTP_PROXY环境变量,将其取消即可 unset HTTP_PROXY https:/ ...
- 1016 Phone Bills (25 分)
1016 Phone Bills (25 分) A long-distance telephone company charges its customers by the following rul ...
- python logging 日志模块的配置和使用
import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(line ...
- oracle 用函数返回对象集合
1.先要声明全局type:并且,字段变量类型要为object,不能为record: (1)CREATE OR REPLACE TYPE "DDD_BY_DEPT_STATISTISC&quo ...
- Apache Doris通过supervisor进行进程管理
下面一段文字是摘自doris官方文档:注:在生产环境中,所有实例都应使用守护进程启动,以保证进程退出后,会被自动拉起,如 Supervisor.如需使用守护进程启动,需要修改各个 start_xx.s ...
- css border-bottom(指定下边线的样式、宽度及颜色)
border-bottom(指定下边线的样式.宽度及颜色) border-bottom: 值: border-bottom-style:值; border-bottom-color: 值; borde ...
- 资源 | 源自斯坦福CS229,机器学习备忘录在集结
在 Github 上,afshinea 贡献了一个备忘录对经典的斯坦福 CS229 课程进行了总结,内容包括监督学习.无监督学习,以及进修所用的概率与统计.线性代数与微积分等知识. 项目地址:http ...
- 基于Linux的Samba开源共享解决方案测试(四)
对于客户端的网络监控如图: 双NAS网关100Mb码率视音频文件的稳定读测试结果如下: 100Mb/s负载性能记录 NAS网关资源占用 稳定写 稳定写 CPU空闲 内存空闲 网卡占用 NAS1 8个稳 ...
- MySQL数据库索引(上)
上一篇回顾: 1.数据页由七部分组成,包括File Header(描述页的信息).Page Header(描述数据的信息).Infimum + Supremum(页中的虚拟数据最大值和最小值).Use ...