[Leetcode 55, Medium] Jump Game
2015-05-09 16:41
417 查看
Problem:
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 =
Analysis:
Solutions:
C++:
Python:
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.
Analysis:
Solutions:
C++:
bool canJump(vector<int>& nums) { if(nums.empty()) return false; if(nums.size() == 1) return true; for(int i = 0; i < nums.size() - 1;) { int max_steps = i; int next_start = i; for(int j = i + 1; j <= i + nums[i]; ++j) { if(j + nums[j] >= nums.size() - 1) return true; if(j + nums[j] >= max_steps) { max_steps = j + nums[j]; next_start = j; } } if(i == next_start) break; i = next_start; } return false; }Java:
Python:
相关文章推荐
- 【LeetCode】(55)Jump Game(Medium)
- LeetCode-55-Jump Game(贪心)-Medium
- leetcode(55):Jump Game
- [array] leetcode-55. Jump Game - Medium
- 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】Jump Game #55
- LeetCode 55 --- Jump Game
- LeetCode 55 Jump Game
- LeetCode55——Jump Game
- Leetcode 55 Jump Game(第七周作业)
- LeetCode55 Jump Game
- LeetCode 55: Jump Game
- LeetCode 55 Jump Game(Python 实现及详解)
- 【LeetCode】C# 55、Jump Game