-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpGame.h
More file actions
48 lines (43 loc) · 1.07 KB
/
JumpGame.h
File metadata and controls
48 lines (43 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**************************************
* Author : luoshikai
* Version : 1.0
* Date : 2013-11-05
* Email : [email protected]
*************************************/
/**************************************
* 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.
*************************************/
class Solution {
public:
bool canJump(int A[], int n)
{
if (n <= 1) return true;
int pos = 0;
while (pos < n-1)
{
if (A[pos] == 0)
{
int i = 1;
while (A[pos] < i)
{
--pos;
++i;
}
if (pos < 0) return false;
}
pos = A[pos] + pos;
}
return true;
}
};