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 ...
随机推荐
- discuz使用
1.discuz登陆错误过多,后台删除ip SELECT * FROM `lang_common_admincp_session` 2.Discuz与UCenter通信失败的解决方法 应用与 UCen ...
- poj 1840 暴力+标记
Description Consider equations having the following form: a1x1 3+ a2x2 3+ a3x3 3+ a4x4 3+ a5x5 3=0 T ...
- 2015GitWebRTC编译实录4
2015.07.17 libg711 编译通过[422/1600 ] CC obj /webrtc/modules/audio_coding/codecs/g711/g711.g711.o[423/1 ...
- leetcode 152. Maximum Product Subarray --------- java
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- leetcode 114 Flatten Binary Tree to Linked List ----- java
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
- PHP防止页面刷新、重复提交数据
PHP防止页面刷新.重复提交数据 (2011-12-09 16:52:45) 转载▼ 标签: it 分类: php技术相关 闲来无事看了一下php session ,又在网上看了防止页面刷新重复提交数 ...
- ExtJS 的一些技巧与问题
1.修改列表(grid)里store的加载url grid.getStore().proxy.conn.url = "xxx.jsp"; grid.getStore().reloa ...
- (译) 强化学习 第一部分:Q-Learning 以及相关探索
(译) 强化学习 第一部分:Q-Learning 以及相关探索 Q-Learning review: Q-Learning 的基础要点是:有一个关于环境状态S的表达式,这些状态中可能的动作 a,然后你 ...
- 斯坦福大学Andrew Ng教授主讲的《机器学习》公开课观后感[转]
近日,在网易公开课视频网站上看完了<机器学习>课程视频,现做个学后感,也叫观后感吧. 学习时间 从2013年7月26日星期五开始,在网易公开课视频网站上,观看由斯坦福大学Andrew Ng ...
- mysql学习之-show table status(获取表的信息)参数说明
--获取表的信息mysql> show table status like 'columns_priv'\G;*************************** 1. row ******* ...