55. Jump Game
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.
===========
非负数组,数组元素表示在当前位置能jump的最大距离,
问:是否能到达最后的位置?
----------
思路:正向贪心的思路,
每一步记住能够到达最远的距离,就好。
=====
code
class Solution {
//本题正向贪心
public:
bool canJump(vector<int>& nums) {
int maxLocation;//当前可能到达的最大位置(下标)
maxLocation = nums[];
int length = nums.size();
for(int i = ;i<length && maxLocation>=i;i++){
///maxLocation>=i 在这里是剪枝,遇到1,2,0.0.0.0.0.0.0....这样直接返回,无需遍历整个数组了。
maxLocation = max(maxLocation,i+nums[i]);
}
return maxLocation >= (length-);
}
};
2,也可以采用爬楼梯方法
思路:
@int max_left
bool canJump{
max_left = nums.size()-;
for(int i = nums.size()-;i>=;i--){
if(nums[i]+i>=max_left) max_left = i;
}
return max_left==?
}
55. Jump Game的更多相关文章
- 55. Jump Game leetcode
55. Jump Game Total Accepted: 95819 Total Submissions: 330538 Difficulty: Medium Given an array of n ...
- [Leetcode][Python]55: Jump Game
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ...
- 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 ...
- 刷题55. Jump Game
一.题目说明 题目55. Jump Game,给定一组非负数,从第1个元素起,nums[i]表示你当前可以跳跃的最大值,计算能否到达最后一个index.难度是Medium. 二.我的解答 非常惭愧,这 ...
- [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 ...
- 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 ...
随机推荐
- meta 标签 关键字 用处
您的个人网站即使做得再精彩,在“浩瀚如海”的网络空间中,也如一叶扁舟不易为人发现,如何推广个人网站, 人们首先想到的方法无外乎以下几种: l 在搜索引擎中登录自己的个人网站 l 在知名网站加入你个人网 ...
- Frameworks , cat,kafka
https://github.com/ServiceStack/ServiceStack https://github.com/ctripcorp/ https://github.com/ctripc ...
- Driver development
Windows Driver Kit (WDK) https://msdn.microsoft.com/en-us/library/windows/hardware/ff557573(v=vs.85) ...
- Linux下编译使用boost库:
Boost是什么不多说, 下面说说怎样在Linux下编译使用Boost的所有模块. 1. 先去Boost官网下载最新的Boost版本, 我下载的是boost_1_56_0版本, 解压. 2. 进入解压 ...
- JSBinding + SharpKit / JavaScript 加载流程
首先,现在的方案是游戏启动就加载全部的 JavaScript 代码. 先看下 StreamingAssets/JavaScript/ 文件夹下的目录结构:
- 通过 Javacore 诊断线程挂起等性能问题
http://www.ibm.com/developerworks/cn/websphere/library/techarticles/1406_tuzy_javacore/1406_tuzy_jav ...
- vb6中webbrowser控件树转换备忘
Dim doc As HTMLDocument Set doc = WebBrowser1.Document Dim inputs As IHTMLElementCollection Set inpu ...
- EDIUS如何实现抠图
有时候我们把素材图片导入到EDIUS中会发现图片的背景有点不合适,这时候就会想替换掉这个背景.那么EDIUS也可以像PS一样替换掉背景吗?答案当然是肯定的,现在小编就带领你们一起来学习EDIUS抠图吧 ...
- SVG中的'defs' and 'use'-可复用的图元定义
在下一个示例中,我使用了defs中的元素之前,定义了如何去展现图元. <?xml version="1.0" standalone="no"?> & ...
- libc-glibc
glibc 和 libc 都是 Linux 下的 C 函数库. libc 是 Linux 下的 ANSI C 函数库:glibc 是 Linux 下的 GUN C 函数库. ANSI C 和 GNU ...