FTPrep: 45 Jump Game 2
来源:互联网 发布:卖家加入淘宝客要求 编辑:程序博客网 时间:2024/05/19 18:00
这题关键是说了一定会reach to end,所以代码相对可以简单点。
代码:
public class Solution { public int jump(int[] nums) { int len=nums.length; if(len==0) return 0; int updatedReach=0; int step=0; int currReach=0; for(int i=0; i<len-1; i++){ // don't need to care about the last step. As the description says, it can always jump to the last; updatedReach=Math.max(updatedReach, i+nums[i]); if(i==currReach){ step++; currReach=updatedReach; } } return step; }}
如果改变要求,说如果达不到end,那么就return 0,这样的话其实更general,参考ganker的代码。在for loop中设置两个出口。一个是end,一个是maxReach
阅读全文
0 0
- FTPrep: 45 Jump Game 2
- FTPrep: 55, Jump Game
- 45、Jump Game II
- 45Jump Game II
- 45-Jump Game II
- 45-Jump Game II
- Leetcode 55 Jump Game && 45 Jump Game II
- [leetcode 45] Jump Game II
- LeetCode(45) Jump Game II
- [LeetCode 45]Jump Game II
- leetcode || 45、 Jump Game II
- leetcode 45:Jump Game II
- 【LeetCode】45:Jump Game II
- leetcode 45: Jump Game II
- LeetCode-45 Jump Game II
- Leetcode#45||Jump Game II
- leetcode-45 Jump Game II
- Leetcode #45 Jump Game II
- synchronized原理
- Markdown使用之如何满足中文阅读习惯,实现首行缩进?
- Linux下动态监测设备拔插程序
- linux解压集合
- Java并发编程:Timer和TimerTask(转载)
- FTPrep: 45 Jump Game 2
- 前端散乱笔记汇总
- HashMap和Hashtable的区别
- 解决 mysql 存储过程 repeat 循环多一次
- 【golang】第三方命令行 cli 的使用
- 手机画图
- 解决win10win7win8系统注册不了大漠插件,注册失败的问题
- 通过java调用短信猫发短信
- C++学习心得