LeetCode 47. Jump Game II
2014-06-30 03:12
288 查看
思路是每次迭代都考察在第step步最多能走到多远(next);在下次迭代则考察第step+1步能走多远。当next >= n-1时停止迭代。
如,输入[2, 3, 1, 1, 4], 则:
第一步访问下标0, 发现最远能到下标2;
第二步访问下标1, 2, 发现最远下标next = 4, next >= n-1, 停止迭代。
有点像BFS.
代码:
如,输入[2, 3, 1, 1, 4], 则:
第一步访问下标0, 发现最远能到下标2;
第二步访问下标1, 2, 发现最远下标next = 4, next >= n-1, 停止迭代。
有点像BFS.
代码:
class Solution { public: int jump(int A[], int n) { int step = 0; for (int i=0, bound=0, next=0; next < n - 1; ++ step) { for ( ; i <= bound; ++ i) { next = max(next, i+A[i]); } bound = next; } return step; } };
相关文章推荐
- 【leetcode】45. Jump Game II
- [LeetCode] Jump Game II
- LeetCode之Jump Game II
- LeetCode | Jump Game II
- Leetcode 55 Jump Game && 45 Jump Game II
- LeetCode Jump Game II
- [Leetcode] Jump Game II
- [LeetCode P45] Jump Game II
- [Leetcode 45] Jump Game II
- [LeetCode][Java] Jump Game II
- [LeetCode] Jump Game II
- leetcode解题报告:45. Jump Game II
- leetcode[45]Jump Game II
- LeetCode 45 Jump Game II
- 《leetCode》:Jump Game II
- [leetcode刷题系列]Jump Game II
- [Leetcode 100] 130 Jump Game II
- Leetcode 45. Jump Game II
- LeetCode Jump Game II
- leetcode 045 —— Jump Game II