FTPrep: 55, Jump Game
2017-09-04 01:18
204 查看
这题和max subarray 有点类似,都是在遍历经过每个点时进行变量的更新,max subarray:
点击打开链接, there are 2 variables there need to be updated, first local, then global, and !! Global is dependent on local !!!靠,这一点我在这里总结出来了!还有一点的话,在max subarray 里要注意的就是难得糊涂,不要就纠结当前点的正负号关系,用这个来做优化,因为这个是起不到什么优化作用的。
这题的话,只有一个变量就是最大距离,没经过一个点都需要更新,然后在下一点的时候进行判断,知道最后没有退出的话就返回true
代码很简单:
class Solution {
public boolean canJump(int[] nums) {
int len=nums.length;
if(len==0) return false;
int maxJump= 0 + nums[0];
for(int i=0; i<len; i++){
if(i>maxJump) return false;
maxJump=Math.max(maxJump, i+nums[i]);
//if(maxJump>=len) return true; adding this operation here actually decrease the efficiency, maybe because in most test case, the ending are pretty late, so not many test case ends early
}
return true;
}
}
点击打开链接, there are 2 variables there need to be updated, first local, then global, and !! Global is dependent on local !!!靠,这一点我在这里总结出来了!还有一点的话,在max subarray 里要注意的就是难得糊涂,不要就纠结当前点的正负号关系,用这个来做优化,因为这个是起不到什么优化作用的。
这题的话,只有一个变量就是最大距离,没经过一个点都需要更新,然后在下一点的时候进行判断,知道最后没有退出的话就返回true
代码很简单:
class Solution {
public boolean canJump(int[] nums) {
int len=nums.length;
if(len==0) return false;
int maxJump= 0 + nums[0];
for(int i=0; i<len; i++){
if(i>maxJump) return false;
maxJump=Math.max(maxJump, i+nums[i]);
//if(maxJump>=len) return true; adding this operation here actually decrease the efficiency, maybe because in most test case, the ending are pretty late, so not many test case ends early
}
return true;
}
}
相关文章推荐
- FTPrep: 45 Jump Game 2
- Leetcode 55 - Jump Game
- 【55】Jump Game
- FTPrep, 13 Roman to integer
- FTPrep, 24 Swap Nodes in Pairs
- FTPrep, 36 Valid Sudoku
- FTPrep, 66 Plus One
- FTPrep, 69 Sqrt(x)
- FTPrep, 72 Edit Distance
- FTPrep, 76 Minimum Window Substring
- FTPrep, 94 Binary Tree Inorder Traversal, TODO: recursive 所有/部分 遍历的模版
- FTPrep, 109 Convert Sorted List to Binary Search Tree
- leetcode 55|45. Jump Game 1|2
- leetcode55 Jump Game
- 第六周:(LeetCode 45 55) Jump Game I II(c++)
- [LeetCode55]Jump Game
- FTPrep, 15 3Sum
- FTPrep, 25 Reverse Nodes in k-Group
- FTPrep, 37 Sukodu Solver
- FTPrep, 56 Merge Intervals