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 ...
随机推荐
- JS从头开始
API:应用程序编程接口 HTML:超文本标记语言 XML:可扩展标记语言 HTML和XML的差别:设计目的不同:XML被设计用来传输和存储数据,其焦点在数据的内容:HTML被设计用来显示数据,其焦点 ...
- 第二篇T语言代码编写技巧
控件事件 控件 控件是对数据和方法的封装.控件可以有自己的属性和方法.属性是控件数据的简单访问者.方法则是控件 的一些简单而可见的功能. 概述 1.控件应用 使用现成的控件来开发应用程序时,控件工作在 ...
- HZAU 17:LCS
17: LCS Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 184 Solved: 43[Submit][Status][Web Board] De ...
- Web服务器IPtables配置
#允许SSH流量(重要) iptables -A INPUT -p tcp --dport 22 -j ACCEPT iptables -A INPUT -p tcp --dport 222 -j A ...
- 【BZOJ1008】【HNOI2008】越狱
以前水过的水题 原题: 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱 1& ...
- 【POJ3904】【P1202】水晶密码
说是莫比乌斯反演,其实只是玩儿玩儿内个miu函数而已…… 原题: wty 打算攻击 applepi 的用来存放机密数据的水晶系统. applepi 早有察觉,于是布置了一个密码系统来防备 wty ...
- java的nio之:java的nio系列教程之FileChannel
一:Java NIO的FileChannel===>Java NIO中的FileChannel是一个连接到文件的通道.可以通过文件通道读写文件. ===>FileChannel无法设置为非 ...
- 账户切换[转自vbird]
我们都是使用一般账号登陆系统的,等有需要进行系统维护或软件升级时才转为 root 的身份来动作. 那如何让一般使用者转变身份成为 root 呢?主要有两种方式喔: 以『 su - 』直接将身份变成 r ...
- HttpServletRequest/HttpServletResponse乱码问题解决
1.request.setCharacterEncoding只对POST请求起作用.GET请求用new String(paramterData.getBytes("iso8859-1&quo ...
- PHP- 深入PHP、Redis连接
pconnect, phpredis中用于client连接server的api. The connection will not be closed on close or end of reques ...