Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b0ceac8

Browse files
Add Solution.java to problems 0055.
1 parent da8436b commit b0ceac8

File tree

4 files changed

+76
-5
lines changed

4 files changed

+76
-5
lines changed

‎solution/0055.Jump Game/README_EN.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Jump Game
2+
3+
Given an array of non-negative integers, you are initially positioned at the first index of the array.
4+
5+
Each element in the array represents your maximum jump length at that position.
6+
7+
Determine if you are able to reach the last index.
8+
9+
10+
## Example 1:
11+
```
12+
Input: [2,3,1,1,4]
13+
Output: true
14+
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
15+
```
16+
17+
## Example 2:
18+
```
19+
Input: [3,2,1,0,4]
20+
Output: false
21+
Explanation: You will always arrive at index 3 no matter what. Its maximum
22+
jump length is 0, which makes it impossible to reach the last index.
23+
```
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class Solution {
2+
23
public boolean canJump(int[] nums) {
3-
int count=0;
4-
for(int i=nums.length-2;i>=0;i--) {
5-
if (nums[i] > count) count = 0;
6-
else count++;
4+
int count = 0;
5+
for (int i = nums.length - 2; i >= 0; i --) {
6+
count = nums[i] > count ? 0 : count + 1;
77
}
8-
return count==0;
8+
return count == 0;
99
}
1010
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
3+
public boolean canJump(int[] nums) {
4+
return solution(nums.length - 1, nums);
5+
}
6+
7+
private boolean solution(int target, int[] nums) {
8+
if (target == 0) return true;
9+
for (int i = target - 1; i >= 0; i--) {
10+
if (nums[i] + i >= target) {
11+
return solution(i, nums);
12+
}
13+
}
14+
return false;
15+
}
16+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
enum Index {
2+
GOOD, BOD, INIT;
3+
}
4+
5+
class Solution {
6+
7+
private Index[] indexs;
8+
9+
public boolean canJump(int[] nums) {
10+
this.indexs = new Index[nums.length];
11+
for (int i = 0; i < indexs.length; i++) {
12+
indexs[i] = Index.INIT;
13+
}
14+
indexs[indexs.length - 1] = Index.GOOD;
15+
return solution(0, nums);
16+
}
17+
18+
private boolean solution(int i, int[] nums) {
19+
if (indexs[i] != Index.INIT) {
20+
return indexs[i] == Index.GOOD;
21+
}
22+
int limit = Math.min(nums[i] + i, nums.length - 1);
23+
for (int j = i + 1; j <= limit; j ++) {
24+
if (solution(j, nums)) {
25+
indexs[i] = Index.GOOD;
26+
return true;
27+
}
28+
}
29+
indexs[i] = Index.BOD;
30+
return false;
31+
}
32+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /