leetcode55
bool canJump(vector<int>& nums) {
int reach = nums[];
for (int i = ; i < nums.size() && reach >= i; i++)
{
if (i + nums[i] > reach)
{
reach = i + nums[i]; //贪心策略
}
}
return reach >= (nums.size() - ) ? true : false;
}
这是贪心算法类型的题目。
补充一个python的实现:
class Solution:
def canJump(self, nums: 'List[int]') -> 'bool':
n = len(nums)
if n == 1:
return True
i = n - 1
j = i - 1
nexti = i
while i>= 0:
tag = False
while j >= 0:
diff = i - j
val = nums[j]
if diff <= val:
nexti = j
tag = True
if tag:
i = nexti
j = i - 1
break
else:
j -= 1
if not tag:
return False
if nexti == 0:
return True
return True
补充一个双指针思路的解决方案,使用python实现:
class Solution:
def canJump(self, nums: 'List[int]') -> bool:
n = len(nums)
j = #可以跳到的最远的索引
for i in range(n):
if i > j:#说明有无法跳跃的索引
return False
j = max(j,i+nums[i])#更新最远的索引
if j >= n - :#达到最右索引
return True
return False
leetcode55的更多相关文章
- [array] leetcode-55. Jump Game - Medium
leetcode-55. Jump Game - Medium descrition Given an array of non-negative integers, you are initiall ...
- [LeetCode55]Jump Game
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- [Swift]LeetCode55. 跳跃游戏 | Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- leetcode55:跳跃游戏
解题思路1: 从头往后找每一个为0的元素,判断这个0能够跳过,所有的0都能跳过,则返回True,否则返回False 解题思路2: 从前往后遍历数组,设置一个访问到当前位置i时最远可调到的距离maxle ...
- leetcode55—Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【1】【经典回溯、动态规划、贪心】【leetcode-55】跳跃游戏
给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4]输出: true解释 ...
- leetcode-55. Jump Game · Array
题面 这个题面挺简单的,不难理解.给定非负数组,每一个元素都可以看作是一个格子.其中每一个元素值都代表当前可跳跃的格子数,判断是否可以到达最后的格子. 样例 Input: [2,3,1,1,4] Ou ...
- Leetcode55. Jump Game跳跃游戏
给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: true ...
- 动态规划——leetcode55、跳跃游戏
1.题目描述: 2.解题方法:动态规划 动态规划解题步骤: 1.确定状态: 最后一步:如果能跳到最后一个下标,我们考虑他的最后一步到n-1(最后一个下标),这一步是从 i 跳过来的,i<n-1 ...
随机推荐
- ACM-ICPC 2018 南京赛区网络预赛B
题目链接:https://nanti.jisuanke.com/t/30991 Feeling hungry, a cute hamster decides to order some take-aw ...
- IO调度算法的理解(转载)
IO调度器(IO Scheduler)是操作系统用来决定块设备上IO操作提交顺序的方法.存在的目的有两个,一是提高IO吞吐量,二是降低IO响应时间.然而IO吞吐量和IO响应时间往往是矛盾的,为了尽量平 ...
- query
- laravel 创建自动化生成数据库
1. 生成 迁移脚本 php artisan make:migration create_users_table --create=users(表名) 当你⽣成⼀个模型时想要顺便⽣成⼀个 数据库迁 ...
- 解决使用angular2路由后,页面刷新后报404错误。
点击路由链接跳转页面是正常的,但是当刷新页面时就出现了404错误. 解决方法如下: 在app.module.ts中添加import: import {HashLocationStrategy,Loca ...
- “必须执行Init_Clk函数,才能采集到二氧化碳接口485数据的问题”的解决
这个问题困扰了我很长一段时间,而且如果这个问题不解决,就有一个无法调和的矛盾:执行Init_Clk函数,能采集到二氧化碳接口485数据,但是功耗大:不执行Init_Clk函数,不能采集到二氧化碳接口4 ...
- cocos2dx粒子系统的简单使用
cocos2dx自带的几种封装好的粒子系统,下面做个简单使用演示. ParticleFire 火焰粒子系统 ParticleFireworks 烟花粒子系统 ParticleSun 太阳粒子系统 Pa ...
- python中序列化模块json和pickle
json模块:json是第三方包,不是系统内置模块,以字符串序列 常用操作有: json.dumps() # 将变量序列化,即将功能性字符转化为字符串 例: >>> import j ...
- angular6新建项目
mkdir angular6project cd angular6project ng new demo 新建一个普通项目 ng new demo --routing 新建一个带路由的项 ...
- spring IOC 和AOP 方面
spring 的2大核心 是Ioc 和 aop spring的依赖注入:在程序运行期间,由外部容器动态的将依赖对象注入到组件中 IOC: 实例化spring容器的二种方法 第一种:在类路径下寻找配 ...