leetCode #55 Jump Game 贪心
来源:互联网 发布:淘宝店怎样增加流量 编辑:程序博客网 时间:2024/06/05 10:06
题目:给一个数组,存的是从这个点最多能往前跳多少,问能否跳到终点
分析:对于一个点能不能到达,主要看前面的点能不能跳到。比如第0个点,如果能跳到k。那么第1个点可能跳到k,也可能跳不到k,我们就可以搜索1~k的点,看能不能跳得更远。如果我们发现[1,k]的点都不能跳得更远了,而且k<n,那么就不能跳到终点了。如果发现有能跳得更远的,比如搜到点b的时候,能跳到m了,那么我们就接着搜[b,m]的范围。如果在整个搜索过程中有发现能跳到>n的,那么就可以到终点。
答案:
class Solution {public: bool canJump(vector<int>& nums) { int n = nums.size(); int currentmax = 0; for(int i = 0; i< n ; i++){ if (i <= currentmax)// we can reach i currentmax = max(currentmax,nums[i] + i);// can we reach high? } return currentmax >= n-1; }};
0 0
- leetCode #55 Jump Game 贪心
- Leetcode 贪心 Jump Game
- [leetcode][贪心] Jump Game
- leetcode---Jump Game---贪心
- leetcode---jump-game---贪心
- LeetCode-55-Jump Game(贪心)-Medium
- LeetCode-55-Jump Game 贪心水题
- Leetcode 贪心 Jump Game II
- [leetcode][贪心] Jump Game II
- LeetCode Jump Game(贪心算法)
- LeetCode Jump Game II(贪心)
- [leetcode]55. Jump Game 贪心
- leetcode 55. Jump Game [贪心]
- LeetCode 55: Jump Game
- [leetcode 55] Jump Game
- LeetCode(55) Jump Game
- [LeetCode 55]Jump Game
- leetcode || 55、Jump Game
- KMP算法实现
- JS获取新浪实时股票行情数据
- 【使用JSOUP实现网络爬虫】修改数据-设置一个元素的HTML内容
- android Jvm 笔记。内存溢出。内存区域划分。垃圾回收气
- Mac配置全局 Composer
- leetCode #55 Jump Game 贪心
- 游戏之数独之最终的结果
- wpa_supplicant无线网络配置
- 解决Nginx的connect() to 127.0.0.1:8080 failed (13: Permission denied) while connect
- 30多条mysql数据库优化方法,千万级数据库记录查询轻松解决
- Android Studio : 修改Debug签名的Key
- 欧拉函数
- php接口开发入门(一)--Mac10.10下环境搭建
- cocos studio设计界面,在代码中寻找按钮,设置监听函数等