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 ...
随机推荐
- day 07 元组,字典和集合等数据类型介绍
元组:就是一个不可变的列表 1.用途,当我们需要记录多个值,并且没有更改的需求的时候,应该使用元组 2定义方式:使用,在 ( ) 中分隔开多个任意类型的值 注:t=("egg",) ...
- Flink实战(1) - Apache Flink安装和示例程序的执行
在Windows上安装 从官方网站下载需要的二进制包 比如我下载的是flink-1.2.0-bin-hadoop2-scala_2.10.tgz,解压后进入bin目录 可以执行bat文件,也可以使用c ...
- spark on yarn运行产生jar包冲突问题
1.1 问题描述 Spark Streaming程序解析protobuf序列化的数据时,--jars 来添加依赖的protobuf-java-3.0.0.jar包,使用local模式程序正常,使用ya ...
- JAVA常用设计模式(一、单例模式、工厂模式)
JAVA设计模式之单例模式 import java.util.HashMap; import java.util.Map; /** * 设计模式之单例模式 * 单例模式(Singleton Patte ...
- js的一些方法
input的值: value.toUpperCase();//value.toUpperCase()把字符窜转换为大写 random方法: Math.floor对数字向下舍入 Math.random( ...
- 421. Maximum XOR of Two Numbers in an Array
这题要求On时间复杂度完成, 第一次做事没什么思路的, 答案网上有不贴了, 总结下这类题的思路. 不局限于这个题, 凡是对于这种给一个 数组, 求出 xxx 最大值的办法, 可能上来默认就是dp, ...
- js 功能
---IE wps excelApp =ActiveXObject("Excel.Application") App.DisplayAlerts = false 不显示警告 App ...
- Linux之cd、pwd、mkdir、rmdir
cd.pwd.mkdir.rmdir 命令功能: 切换到指定的目录,可用绝对路径和相对路径 命令格式: cd directory 命令参数: 无 命令实例: 1.切换到/bin目录 vbird@Ubu ...
- Java环境配置之JDK安装
一.下载 现在JDK的版本很多.我下载的是jdk1.7 以下链接是jdk1.8的 http://www.oracle.com/technetwork/java/javase/downloads/jdk ...
- 使用lite-server
进入项目根目录,执行下列步骤 安装lite-server npm install lite-server 新建配置文件bs-config.json bs-config.json中可以: 指定监听的端口 ...