Skip to content

Commit f8cabd2

Browse files
committed
some code
1 parent fb31279 commit f8cabd2

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Jump Game.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Given an array of non-negative integers, you are initially positioned at the first index of the array.
2+
3+
Each element in the array represents your maximum jump length at that position.
4+
5+
Determine if you are able to reach the last index.
6+
7+
For example:
8+
A = [2,3,1,1,4], return true.
9+
10+
A = [3,2,1,0,4], return false.
11+
12+
public class Solution {
13+
public boolean canJump(int[] A) {
14+
if (A.length <= 1) return true;
15+
int curMax = 0;
16+
int max = 0;
17+
for (int i = 0; i < A.length - 1; i++) {
18+
if (i > max) break;
19+
curMax = A[i] + i;
20+
if (curMax > max) {
21+
max = curMax;
22+
}
23+
if (max >= A.length - 1) return true;
24+
}
25+
return false;
26+
}
27+
}

0 commit comments

Comments
 (0)