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 ...
随机推荐
- mod_fcgid FcgidMaxRequestLen 131072 问题
mod_fcgid: HTTP request length 136136 (so far) exceeds MaxRequestLen (131072) 原来是fastcgi模式下的设置问题,需 ...
- 返回指针的函数 ------ 指针函数(pointer function)
指针函数: 其本质是一个函数, 其函数返回值为某一类型的指针. 定义形式: 类型 *指针变量名(参数列表): 例如: int *p(int i,int j); p是一个函数名,该函数有2个整形参数,返 ...
- Windows10下安装MySQL8.0
1:首先去官网下载安装包 下载地址:https://dev.mysql.com/downloads/mysql/ 这是我下载版本 2:将解压文件解压到你安装的目录:E:\mysql-8.0.11-wi ...
- [LeetCode&Python] Problem 504. Base 7
Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "202&q ...
- memcache 应用场景
一..memcache应用场景 1.应用场景一: 缓解数据库压力,提高交互速度.它的一个总原则是将经常需要从数据库读取的数据缓存在memcached中.这些数据也分为几类: (1).经常被读取并且实时 ...
- C++学习(三十六)(C语言部分)之 链表2
测试代码笔记如下: #include<stdio.h> #include<stdlib.h> typedef struct node { int data;//数据 struc ...
- form表单以及内嵌框架标签
今关于今天所学习的东西又复杂又简单,上午学习了form表单,也是挺简单的:搭配table表格使用也是非常熟练. 下午讲了讲给网页内嵌框架标签以及在内嵌框架标签中添加其他的网页:还有在内嵌框架标签中添加 ...
- 深入学习Motan系列(三)——服务发布
袋鼠回头看了看文章,有些啰嗦,争取语音简练,不断提高表达力!袋鼠奋起直追! 注:此篇文章,暂时为了以后时间线排序的需要,暂时发表出来,可是仍然有许多地方需要改写.自己打算把服务端发布,客户端订阅都搞定 ...
- H3C交换机配置vlan
一,内存二,硬盘(分区,数据量大小)三,电源线,网络线四,raid(raid0,raid1,raid5)五,装系统(系统版本,分区)六,配置网络 1.创建用户 system-view #进入配置loc ...
- 11g R2 RAC 虚拟机
虚拟机安装RAC文档 本文档包含内容 一:安装系统 二:各节点配置系统参数 三:虚拟机创建共享存储 四:配置磁盘绑定 五:安装GRID 六:创建ASM DG 七:安装database 八:安装碰到的问 ...