leetcode-55. Jump Game · Array
题面
这个题面挺简单的,不难理解。给定非负数组,每一个元素都可以看作是一个格子。其中每一个元素值都代表当前可跳跃的格子数,判断是否可以到达最后的格子。
样例
Input: [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.Input: [,,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum
jump length is 0, which makes it impossible to reach the last index.
算法
只要存在一条路径可以到达最后就说明可以。我们可以从后往前看,只要前面存在元素索引加上其元素值大于目标元素索引值,就代表从前面格子可以跳到目标格子。只要我们从后往前判断满足该条件就向前滑动,即目标格子更新成为前面的格子,直到数组头则代表都能走通;否则,走不通。
倒叙遍历数组,定义target为数组尾,判断索引值+元素值 > target ?target = 该索引 :不做处理;
便利结束,判断 target = 0 ? true : false
评价
时间复杂度:O(n) 只需要遍历一次数组。
空间复杂度:O(1)
源码
class Solution {
public:
bool canJump(vector<int>& nums) {
int len = nums.size();
if(len <= )
return true;
int target = len -;
for(int i = target-; i >= ; i--)
{
if(nums[i] + i >= target)
target = i;
}
return target == ;
}
};
leetcode-55. Jump Game · Array的更多相关文章
- 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 ...
- [LeetCode] 55. Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Jump Game 的三种思路 - leetcode 55. Jump Game
Jump Game 是一道有意思的题目.题意很简单,给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置. 比如: A = ...
- 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_ Medium tag: Dynamic Programming
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode 55. Jump Game
我一开始认为这是一道DP的题目.其实,可以维护一个maxReach,并对每个元素更新这个maxReach = max(maxReach, i + nums[i]).注意如果 i>maxReach ...
- [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 (Array; Greedy)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- python 基础之确认文件是否存在
def check_exist_bills(): file_dir=os.listdir('../db') bills_db_list=[] for item in file_dir: if item ...
- maven中央仓库的配置在哪里?superpom是什么?中央仓库查找三方包
maven的superpom 每个项目都默认继承的pom 位置 $M2_HOME/lib/maven-model-builder.jar 使用tar -xvf解压后,grep -r central 搜 ...
- ALBPM Service Config
ALBPM Config About ALBPM Studio Msg , JSP and webResources together deploy services. Msg "D ...
- C/C++语言,自学资源,滚动更新中……
以下教学视频中,缺少对“字符串”技术的讨论,大家注意看书. 一维数组,及其举例:(第四版)P77~P85,书上这部分内容写的很好,很详细,尤其是这里列出来的每一个例子都要仔细去看一看,对你会很有帮助. ...
- Mariadb/Mysql命令行常用命令
一.初始化等 1.登陆数据库方法 mysql -u 用户名 -p 用户密码 2.修改root及用户密码 use mysql; update user set password=password( ...
- C#使用KingAOP实现AOP面向切面编程一
AOP面向切面编程(Aspect Oriented Programming),是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. 实现AOP主要由两种方式,一种是编译时静态植入,优点是 ...
- php取上个月月初和月末时间戳
$thismonth = date('m');$thisyear = date('Y');if ($thismonth == 1) { $lastmonth = 12; $lastyear = $th ...
- centos 防火墙相关命令
防火墙关闭: systemctl stop firewalld systemctl disable firewalld 重启防火墙: systemctl enable firewalld system ...
- airflow安装rest api插件发现airflow webserver服务不能启动的解决办法
安装插件airflow-rest-api 1)获取 wget https://github.com/teamclairvoyant/airflow-rest-api-plugin/archive/ma ...
- 第一坑:class编译版本
这个坑是刚去公司的时候,公司项目运行环境的jdk版本是1.5,当时我编译版本是1.7,然后放上去一直报找不到class的错误,我硬是找了半天,后来才听说要用1.5版本编译.