Skip to content

Commit b60d789

Browse files
author
王俊超
committed
commit
1 parent 6046579 commit b60d789

File tree

4 files changed

+66
-45
lines changed

4 files changed

+66
-45
lines changed

.idea/modules.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Author: 王俊超
3+
* Date: 2015-06-19
4+
* Time: 19:29
5+
* Declaration: All Rights Reserved !!!
6+
*/
7+
public class Solution {
8+
/**
9+
* <pre>
10+
* Given an array of non-negative integers, you are initially positioned at the
11+
* first index of the array.
12+
*
13+
* Each element in the array represents your maximum jump length at that position.
14+
*
15+
* Determine if you are able to reach the last index.
16+
*
17+
* For example:
18+
* A = [2,3,1,1,4], return true.
19+
* A = [3,2,1,0,4], return false.
20+
*
21+
* 题目大意:
22+
* 给定的非负整数的数组,则最初定位在数组的第一个位置。数组中的每个元素都代表你的最大跳跃长度在那个位置。
23+
* 决定你是否能到达最后一个索引。
24+
*
25+
* </pre>
26+
*
27+
* @param nums
28+
* @return
29+
*/
30+
public boolean canJump(int[] nums) {
31+
return canJump2(nums);
32+
}
33+
public boolean canJump1(int[] nums) {
34+
if (nums == null || nums.length < 2) {
35+
return true;
36+
}
37+
38+
// 最后可以移动的位置
39+
int lastPos = 0;
40+
41+
// 处理每一个位置
42+
for (int i = 0; i < nums.length; i++) {
43+
// i不能比lastPos大,否则说明不能走到i,走不到i也就不能走到最后
44+
// 如果在i位置可以移动的距离比已经记录到的最大距离还要大,那么更新最大的移动距离
45+
if (i <= lastPos && i + nums[i] > lastPos ) {
46+
lastPos = i + nums[i];
47+
} else if (i > lastPos) {
48+
return false;
49+
}
50+
}
51+
52+
// 最后的位置必然可以达到最后
53+
return lastPos >= nums.length - 1;
54+
}
55+
56+
public boolean canJump2(int[] nums) {
57+
int lastPos = nums.length - 1;
58+
for (int i = nums.length - 1; i >= 0; i--) {
59+
if (i + nums[i] >= lastPos) {
60+
lastPos = i;
61+
}
62+
}
63+
return lastPos == 0;
64+
}
65+
}

【055未】【JumpGame】/src/Solution.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

【055未】【JumpGame】/【055】【JumpGame】.iml

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)