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 ...
随机推荐
- Java RSA分段加密
我们通过Java进行RSA加密的时候,可能会出现如下问题: /** * 私钥加密 * * @param data 待加密数据 * @param key 密钥 * @return byte[] 加密数据 ...
- LeetCode_27. Remove Element
27. Remove Element Easy Given an array nums and a value val, remove all instances of that value in-p ...
- UIwindow ---密码框
程序运行显示结果如下 : 验证密码输入错误显示如下: 代码如下 : 1> //// PasswordInputWindow.m// UIWindow--密码框//// Created by ...
- node.js使用superagent实现模拟登陆功能(包含下载验证码功能)
superagent版本:3.8.3 样例代码: var process = require('process'); var superagent = require('superagent'); v ...
- wordpress通过$wpdb获取一个分类下所有的文章
在wordpress程序根目录下新建一个php文件,粘贴下面的代码 如下面的代码注释,修改$CID这个分类id,就可以获取这个分类下的文章了.这个查询需要联合三个表wp_posts.wp_term_r ...
- iCMSv7.0.15后台database.admincp文件仍存在SQL注入漏洞
闲着无聊,国庆时间没事做,又在Q群看到这种公告,只好下个icms慢慢玩.(PS:医院和学校居然都关网站了) 无奈自己太菜,审不出问题.只好上网百度icms之前的漏洞.然后居然成功在iCMSv7.0.1 ...
- 【miscellaneous】视频浓缩摘要简介
视频摘要,就是以自动或者半自动的方式,通过分析视频的结构和内容存在的时空冗余,从原始视频中提取有意义的片段,将它们以某种特点的方式重新组合成紧凑的.能够充分表现视频语义内容的浓缩视频. 一.静态视频摘 ...
- Linux与windows的对比
Linux与windows的对比 声明:下面的内容总结自实验楼的linux教程,特在此说明. Linux与windows一样,是一个操作系统. Linux与Windows的区别 软件与支持 windo ...
- TCP 客户端编程
1.Qt中TCP客户端编程 对Qt编程而言,网络只是数据传输的通道: Qt提供了QTcpSocket类(封装了TCP协议细节): 将QTcpSocket的对象当做黑盒使用,进行数据首发. 1.1QTc ...
- S1. Android 功能大全
[概述] 这篇文章主要描述安卓开发中可能实现的功能点. [准备工作] IDE:Android Studio,简单操作 如何创建一个 Android 项目 Android 项目结构分析 Androidm ...