Created
February 17, 2013 12:06
-
-
Save pdu/4971233 to your computer and use it in GitHub Desktop.
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. http://leetcode.com/onlinejudge#questio…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| bool canJump(int A[], int n) { | |
| int dis = 0; | |
| for (int i = 0; i < n; ++i) { | |
| if (i <= dis) | |
| dis = max(dis, i + A[i]); | |
| else | |
| break; | |
| if (dis >= n - 1) | |
| break; | |
| } | |
| return dis >= n - 1; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment