Skip to content

Commit 93d7770

Browse files
committed
55. Jump Game
1 parent 988e2f3 commit 93d7770

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

markdown/55. Jump Game.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
### [55\. Jump Game](https://leetcode.com/problems/jump-game/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given an array of non-negative integers, you are initially positioned at the first index of the array.
7+
8+
Each element in the array represents your maximum jump length at that position.
9+
10+
Determine if you are able to reach the last index.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: [2,3,1,1,4]
16+
Output: true
17+
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
18+
```
19+
20+
**Example 2:**
21+
22+
```
23+
Input: [3,2,1,0,4]
24+
Output: false
25+
Explanation: You will always arrive at index 3 no matter what. Its maximum
26+
  jump length is 0, which makes it impossible to reach the last index.
27+
```
28+
29+
30+
#### Solution
31+
32+
Language: **Java**
33+
34+
```java
35+
class Solution { // 问题可以转化为找到可以跳到的最远的距离
36+
   public boolean canJump(int[] nums) {
37+
       int max = 0;
38+
       for (int i = 0; i < nums.length; i++) {
39+
           if (i > max) return false; // 如果最远距离到达不了当前索引,没有必要继续走了
40+
           max = Math.max(max, nums[i] + i);
41+
      }
42+
       return true;
43+
  }
44+
}
45+
```
46+
![](http://ww1.sinaimg.cn/large/006tNc79ly1g4x1rsw78jj318w0u0gqu.jpg)

src/main/java/leetcode/_55_/Main.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package leetcode._55_;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Created by zhangbo54 on 2019-03-04.
7+
*/
8+
public class Main {
9+
public static void main(String[] args) {
10+
Solution solution = new Solution();
11+
int[] nums = {2,0,6,9,8,4,5,0,8,9,1,2,9,6,8,8,0,6,3,1,2,2,1,2,6,5,3,1,2,2,6,4,2,4,3,0,0,0,3,8,2,4,0,1,2,0,1,4,6,5,8,0,7,9,3,4,6,6,5,8,9,3,4,3,7,0,4,9,0,9,8,4,3,0,7,7,1,9,1,9,4,9,0,1,9,5,7,7,1,5,8,2,8,2,6,8,2,2,7,5,1,7,9,6};
12+
boolean result = solution.canJump(nums);
13+
System.out.println(result);
14+
}
15+
}
16+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package leetcode._55_;
2+
3+
class Solution { // 问题可以转化为找到可以跳到的最远的距离
4+
public boolean canJump(int[] nums) {
5+
int max = 0;
6+
for (int i = 0; i < nums.length; i++) {
7+
if (i > max) return false; // 如果最远距离到达不了当前索引,没有必要继续走了
8+
max = Math.max(max, nums[i] + i);
9+
}
10+
return true;
11+
}
12+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
### [55\. Jump Game](https://leetcode.com/problems/jump-game/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given an array of non-negative integers, you are initially positioned at the first index of the array.
7+
8+
Each element in the array represents your maximum jump length at that position.
9+
10+
Determine if you are able to reach the last index.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: [2,3,1,1,4]
16+
Output: true
17+
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
18+
```
19+
20+
**Example 2:**
21+
22+
```
23+
Input: [3,2,1,0,4]
24+
Output: false
25+
Explanation: You will always arrive at index 3 no matter what. Its maximum
26+
  jump length is 0, which makes it impossible to reach the last index.
27+
```
28+
29+
30+
#### Solution
31+
32+
Language: **Java**
33+
34+
```java
35+
class Solution { // 问题可以转化为找到可以跳到的最远的距离
36+
   public boolean canJump(int[] nums) {
37+
       int max = 0;
38+
       for (int i = 0; i < nums.length; i++) {
39+
           if (i > max) return false; // 如果最远距离到达不了当前索引,没有必要继续走了
40+
           max = Math.max(max, nums[i] + i);
41+
      }
42+
       return true;
43+
  }
44+
}
45+
```
46+
![](http://ww1.sinaimg.cn/large/006tNc79ly1g4x1rsw78jj318w0u0gqu.jpg)

0 commit comments

Comments
 (0)