55. Jump Game
2016-06-23 18:42
363 查看
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 =
A =
===========
非负数组,数组元素表示在当前位置能jump的最大距离,
问:是否能到达最后的位置?
----------
思路:正向贪心的思路,
每一步记住能够到达最远的距离,就好。
=====
code
2,也可以采用爬楼梯方法
思路:
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[0]; int length = nums.size(); for(int i = 0;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-1); } };
2,也可以采用爬楼梯方法
思路:
@int max_left bool canJump{ max_left = nums.size()-1; for(int i = nums.size()-2;i>=0;i--){ if(nums[i]+i>=max_left) max_left = i; } return max_left==0? }
相关文章推荐
- vSphere部署系列之05――虚拟机的创建和设置
- javase基础之if语法实例
- 数据结构实验之链表九:双向链表
- 用CSS样式制作导航菜单
- Linux新增磁盘、分区、格式化和自动挂载
- oracle查找重复记录
- 为Elasticsearch添加中文分词,对比分词器效果
- hive 窗口函数 应用场景与实战详解
- SLF4J日志类库
- Quartz 2.2.x CronTrigger Tutorial
- 利用Drawable资源 定制改变外观的Seekbar
- [剑指Offer] 第4章课后题详解
- HDU 3526 最小割
- cv/dl/cl领域的实验室官网/牛人主页/技术论坛/比赛数据库/好玩的东西
- css的行高示例
- MBProgressHUD实现分析
- poj2478【欧拉函数】
- 关键词权重计算算法 - TF-IDF
- Nginx 安装配置
- Linux系统备份与还原