leetcode 55. Jump Game
2017-01-06 09:30
393 查看
参考了http://www.cnblogs.com/zichi/p/4808025.html
遍历数组,不断寻找能到达的最右端的位置,直到下标比最右端位置大为止。最后判断是否能到达最后一个位置。
遍历数组,不断寻找能到达的最右端的位置,直到下标比最右端位置大为止。最后判断是否能到达最后一个位置。
public class Solution { /* * public boolean canJump(int[] nums) { int i; int rightMost = 1; if(nums.length==0 || nums == null) return false; for(i=0;i<nums.length;i++) { if(i+1>rightMost) break; rightMost = Math.max(rightMost, i+1+nums[i]); } if(rightMost >= nums.length) return true; else return false; } */ public boolean canJump(int[] nums) { int i; int rightMost = 0; if(nums.length==0 || nums == null) return false; for(i=0;i<nums.length;i++) { if(i>rightMost) break; rightMost = Math.max(rightMost, i+nums[i]); } if(rightMost >= nums.length-1) return true; else return false; } public static void main(String args[]) { //int[] nums = {3,2,1,1,4}; int[] nums = {0}; boolean res = new Solution().canJump(nums); System.out.println(res); } }
相关文章推荐
- LeetCode *** 55. Jump Game
- leetcode 55. Jump Game
- LeetCode 55. Jump Game
- LeetCode 55. Jump Game
- Leetcode 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
- leetcode题解-55. Jump Game && 56. Merge Intervals
- leetcode.array--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
- LeetCode 55. Jump Game
- 第五周:[Leetcode]55. Jump Game