Java for LeetCode 055 Jump Game
2015-05-15 11:05
405 查看
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 =
解题思路:
参考Java for LeetCode 045 Jump Game II把返回值改为boolean即可,JAVA实现如下:
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.
解题思路:
参考Java for LeetCode 045 Jump Game II把返回值改为boolean即可,JAVA实现如下:
static public boolean canJump(int[] nums) { int index=0,maxStepIndex=0,start=0; while(nums.length>1){ for(int i=start;i<=index+nums[index];i++){ if(i+nums[i]>=nums.length-1) return true; if(i+nums[i]>=nums[maxStepIndex]+maxStepIndex) maxStepIndex=i; } start=index+nums[index]+1; if(index==maxStepIndex) return false; index=maxStepIndex; } return true; }
相关文章推荐
- LeetCode(055) Jump Game (Java)
- Java for LeetCode 189 Rotate Array
- 【LeetCode】 055. Jump Game
- Java for LeetCode 200 Number of Islands
- Java for LeetCode 060 Permutation Sequence
- Java for LeetCode 065 Valid Number
- Java for LeetCode 209 Minimum Size Subarray Sum
- Java for LeetCode 034 Search for a Range
- Java for LeetCode 079 Word Search
- Java for LeetCode 229 Majority Element II
- Java for LeetCode 090 Subsets II
- Java for LeetCode 133 Clone Graph
- Java for LeetCode 140 Word Break II
- Java for LeetCode 146 LRU Cache 【HARD】
- Java for LeetCode 113 Path Sum II
- Java for LeetCode 117 Populating Next Right Pointers in Each Node II
- Java for LeetCode 227 Basic Calculator II
- Java for LeetCode 173 Binary Search Tree Iterator
- Java for LeetCode 121 Best Time to Buy and Sell Stock
- Java for LeetCode 190 Reverse Bits