Jump Game
2015-08-28 06:17
253 查看
原网址:http://blog.csdn.net/linhuanmars/article/details/21354751
发现递归跟动规的知识都还给老师了。。最近要大补一下。
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 =
代码:
发现递归跟动规的知识都还给老师了。。最近要大补一下。
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.
代码:
package codes; public class CanJump { public static void main(String[] args) { // A = [2,3,1,1,4], return true. int [] nums = {2,3,1,1,4}; System.out.println(new CanJump().canJump(nums)); } public boolean canJump(int[] nums) { if (nums.length == 0 || nums == null) return false; int reach = 0; //还必须要保证 是能够取到的 for(int i=0;i< nums.length && i<=reach;i++){ reach=Math.max(reach, i+nums[i]); } if(reach < nums.length-1) return false; return true; } }
相关文章推荐
- 微信读书App来了 小伙伴们快去占榜吧
- Docker Compose—简化复杂应用的利器
- Linked List
- 工厂方法模式——23种设计模式综合实例应用
- 面向对象编程所需要遵循的原则
- 开放封闭原则OCP
- 2nd Round Google Interview
- leedcode做题总结, 题目Maximum Subarray II & Maximum Subarray Difference
- [LeetCode#210]Course Schedule II
- Js 和 PHP 中保留小数点后X位数的方法 toFixed、round、number_format、sprintf
- 找一找自己和大神之间的差距
- Leetcode #235 Lowest Common Ancestor of a Binary Search Tree
- Calculator| ||
- Leetcode #191 Number of 1 Bits
- Android Design Support Library之TabLayout
- 让Style帮你减少JAVA的错误
- vijos1058粘贴文本
- Debian8.1.0的fcitx输入法补充安装方可使用
- 使用m2Eclipse 通过 maven 自动部署webapp到weblogic 12c
- [LeetCode#207]Course Schedule