55. Jump Game
来源:互联网 发布:2017非农数据公布时间 编辑:程序博客网 时间:2024/04/30 12:48
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
.
思路:有点像动态规划的思路,要求这个数组最远可以跳跃到的距离,如果最远的跳跃范围大于等于size-1,就可以跳到,返回true,否则返回false。用一个total值表示最远可以到达的距离,逐步更新,则到达第i步时可以到达的最远距离为total = max(toal, A[i]+i)。前提:total此时表示i-1步之后可以到达的最远距离,i在total之内,因为如果i不在total之内,则跟本跳不到i步,也就不用比total和A[i]+i了。
class Solution {public:bool canJump(vector<int>& nums) {int total=0;//记录总共能够跳跃的最远距离,逐步更新。for (int i = 0; i <= total && i < nums.size(); i++){total = max(total, nums[i] + i);}return total >= nums.size() - 1;}};
0 0
- 55. Jump Game && 45. Jump Game II
- 55. Jump Game. 45. Jump Game II
- [LeetCode]55.Jump Game
- LeetCode 55.Jump Game
- LeetCode --- 55. Jump Game
- [Leetcode] 55. Jump Game
- [leetcode] 55.Jump Game
- 55. Jump Game
- 55. Jump Game
- [leetcode] 55.Jump Game
- *LeetCode 55. Jump Game
- LeetCode 55. Jump Game
- [leetcode] 55. Jump Game
- 55. Jump Game
- leetcode 55. Jump Game
- 55. Jump Game LeetCode
- 【LeetCode】55. Jump Game
- 55. Jump Game
- Qt之QToolButton
- Android页面跳转器--消除跳转时Activity之间的耦合性
- c++之string类型
- C++基本简介
- SQL
- 55. Jump Game
- Java之Vector向量类实现自动动态增长的对象数组-类似动态数组
- 关于集合思想,人机猜数
- 我学习过的Android GitHub开源项目
- TextView里限制输入字数的方法
- Myeclipse格式化不换行的设置
- centos6.5环境下svn服务器和客户端配置实用详解
- Angular $http拦截器介绍与使用
- 第132课: Spark Streaming源码经典解读系列之七:Executro容错工作内幕源码解密