【一天一道LeetCode】#55. Jump Game
一天一道LeetCode系列
(一)题目
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
For example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
(二)解题
思路可以参考这篇博文:【一天一道LeetCode】#45. Jump Game II
考虑在O(n)复杂度的情况下解决此题。用两个参数来lastmax和curmax来存储i前面能跳到的最远距离和当前能跳到的最远距离,如果此刻i还在lastmax之内,就要更新lastmax的值为max(lastmax,curmax)
/*
代码很简单,主要注意两个点:
1、i小于lastmax才能改变lastmax的值
2、是否能跳到重点取决于lastmax的值
*/
class Solution {
public:
bool canJump(vector<int>& nums) {
int lastmax = 0;
int curmax = 0;
for(int i = 0 ; i < nums.size() ; i++)
{
curmax = i+nums[i];
if(i<=lastmax)
{
lastmax = max(lastmax,curmax);
}
}
if(lastmax >=nums.size()-1) return true;
else return false;
}
};
【一天一道LeetCode】#55. Jump Game的更多相关文章
- 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 ...
- 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
我一开始认为这是一道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_ 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 解题思路
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 ...
- LeetCode: 55. Jump Game(Medium)
1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...
随机推荐
- PHP MySQL 创建数据表
PHP 创建 MySQL 表 一个数据表有一个唯一名称,并有行和列组成. 使用 MySQLi 和 PDO 创建 MySQL 表 CREATE TABLE 语句用于创建 MySQL 表. 我们将创建一个 ...
- Gi之(二)基础命令
三个工作区域 使用Git之前,首先要弄清Git的三个管理区域,有助于理解Git的运行原理,以及每个Git命令对文件造成的影响. 对于任何一个文件,在本地的Git内部都有三种状态: l 已修改(mo ...
- 将Gradle项目发布到Jcenter和Maven Central
Jcenter和Maven Central 为了方便我们理解Android studio是如何帮助我们获取开源库的,我们需要理清几个概念.Apache Maven是Apache开发的一个工具,提供了用 ...
- 不应滥用named let
> (define (f x) x) > (define (g x) (let rec((x x)) x)) > (define a '(1 2 3)) > (f a) ( ) ...
- 下载Android源代码编译错误总结
错误1: prebuilts/sdk/api/18.txt:22055: error 9: Removed public method android.telephony.gsm.SmsMessage ...
- android获取短信并自动填充
package com.velo.quanquan.util; import java.util.regex.Matcher; import java.util.regex.Pattern; impo ...
- BeanUtils 读取数据
前两篇文章都是关于setProperty的,下面来说一个关于getProperty 的小案例.如下: MyClass.java package beanutils; public class MyCl ...
- Android 系统自动重启Bug(高通平台)
点击打开链接 最近客户反馈了一个Bug,我们的系统用着用着会自动重启,尤其是在拨号的时候极容易死机或者进入下载模式.根据老大和高通的支持得到了一个解决方案. 在Android系统中,有这么一个文件夹: ...
- 20ViewPager demo1,2:接收ViewPager展示View的使用
Demo1 MainActivity .JAVA package com.qf.day20_viewpager_demo1; import java.util.ArrayList; import ja ...
- Swift中声明协议中的class关键字的作用
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 最近在Cocos2D编程for Swift中看到以下一个代码片 ...