Skip to content

Commit 692ee5c

Browse files
author
tanfanhua
committed
54,55
1 parent ae4b8e7 commit 692ee5c

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
public class Solution {
2+
3+
public static void main(String[] args) {
4+
Solution solution = new Solution();
5+
System.out.println(solution.canJump2End(new int[]{1, 2, 3, 4}));
6+
System.out.println(solution.canJump2End(new int[]{3, 1, 0, 4}));
7+
System.out.println(solution.canJump2End(new int[]{3, 2, 1, 0, 4}));
8+
System.out.println(solution.canJump2End(new int[]{2, 3, 1, 1, 4}));
9+
}
10+
11+
public boolean canJump2End(int[] nums) {
12+
if (nums == null || nums.length == 0) {
13+
return false;
14+
}
15+
int step = 1;
16+
int index = 0;
17+
while (index < nums.length && step > 0) {
18+
step = nums[index];
19+
int maxIndex = index;
20+
int maxStep = index;
21+
for (int i = 1; i <= step; i++) {
22+
if (index + i >= nums.length) {
23+
return true;
24+
}
25+
if (nums[index + i] + index + i > maxStep) {
26+
maxIndex = index + i;
27+
maxStep = nums[index + i] + index + i;
28+
}
29+
}
30+
index = maxIndex;
31+
step = nums[maxIndex];
32+
}
33+
if (step == 0) {
34+
return false;
35+
}
36+
37+
return true;
38+
}
39+
}

src/com/blankj/medium/_0054/Solution.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import java.util.Arrays;
21
import java.util.List;
32

43
public class Solution {
54

65
public static void main(String[] args) {
76
Solution s = new Solution();
8-
// System.out.println(List.of(s.spiralTravel(new int[][]{{1, 2, 3}, {4, 5, 6}})));
7+
System.out.println(List.of(s.spiralTravel(new int[][]{{1, 2, 3}, {4, 5, 6}})));
98
System.out.println(List.of(s.spiralTravel(new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}})));
10-
// System.out.println(List.of(s.spiralTravel(new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}})));
9+
System.out.println(List.of(s.spiralTravel(new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}})));
1110
}
1211

1312
public Integer[] spiralTravel(int[][] numsArray) {

0 commit comments

Comments
 (0)