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的更多相关文章

  1. [array] leetcode-55. Jump Game - Medium

    leetcode-55. Jump Game - Medium descrition Given an array of non-negative integers, you are initiall ...

  2. [LeetCode55]Jump Game

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  3. [Swift]LeetCode55. 跳跃游戏 | Jump Game

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  4. leetcode55:跳跃游戏

    解题思路1: 从头往后找每一个为0的元素,判断这个0能够跳过,所有的0都能跳过,则返回True,否则返回False 解题思路2: 从前往后遍历数组,设置一个访问到当前位置i时最远可调到的距离maxle ...

  5. leetcode55—Jump Game

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  6. 【1】【经典回溯、动态规划、贪心】【leetcode-55】跳跃游戏

    给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4]输出: true解释 ...

  7. leetcode-55. Jump Game · Array

    题面 这个题面挺简单的,不难理解.给定非负数组,每一个元素都可以看作是一个格子.其中每一个元素值都代表当前可跳跃的格子数,判断是否可以到达最后的格子. 样例 Input: [2,3,1,1,4] Ou ...

  8. Leetcode55. Jump Game跳跃游戏

    给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: true ...

  9. 动态规划——leetcode55、跳跃游戏

    1.题目描述:  2.解题方法:动态规划 动态规划解题步骤: 1.确定状态: 最后一步:如果能跳到最后一个下标,我们考虑他的最后一步到n-1(最后一个下标),这一步是从 i 跳过来的,i<n-1 ...

随机推荐

  1. TensorFlow函数:tf.random_shuffle

    tf.random_shuffle 函数 random_shuffle( value, seed=None, name=None ) 定义在:tensorflow/python/ops/random_ ...

  2. MySQL:函数

    函数 一.数学函数 1.绝对值函数ABS(x): x为插入的数据,返回绝对值 2.返回圆周率函数PI(): 无需插入数据,返回圆周率的值,默认为小数点后6位 3.平方根函数SQRT(x): 返回非负数 ...

  3. C#磁性窗体设计

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  4. grep、head和tail

    一.请给出打印test.txt内容时,不包含oldboy字符串的命令 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Globa ...

  5. C++11 类型后置语法

    #include <iostream> #include <typeinfo> #include <type_traits> using namespace std ...

  6. Codeforces 215D. Hot Days(贪心)

    题意 有nnn个地区和mmm个学生,在第iii个地区时,车上有kik_iki​个学生,车内温度(当前城市的温度tit_iti​+当前车上的学生kik_iki​)不能超过TiT_iTi​,否则,赔偿每个 ...

  7. centos安装多个tomcat

    1.参考前文安装jdk.第一个tomcat 2.安装第二个tomcat后,修改/etc/profile vi /etc/profile #tomcat1 export CATALINA_BASE=/o ...

  8. PythonStudy——列表的常用操作 List of common operations

    # 1.索引取值: 列表名[index] s1 = [1, 3, 2] print(s1[0]) print(s1[-1]) # 2.列表运算: 得到的是新list s2 = [1, 2, 3] pr ...

  9. MySQL Event--Event and EventScheduler

    MySQL Event 创建EVENT语法: CREATE [DEFINER = { user | CURRENT_USER }] EVENT [IF NOT EXISTS] event_name O ...

  10. java_oop_类

    类的初始化顺序    再论类的组成    类的初始化顺序详解        变量        实例变量(成员变量)        类变量(静态变量)    方法        实例方法       ...