Skip to content

Commit 5ac3a31

Browse files
authored
45.Jump-贪心算法
1 parent e901c3e commit 5ac3a31

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Jump.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int jump(int[] nums) {
3+
int length = nums.length;
4+
int end = 0;
5+
int maxPosition = 0;
6+
int steps = 0;
7+
for (int i = 0; i < length - 1; i++) {
8+
maxPosition = Math.max(maxPosition, i + nums[i]);
9+
if (i == end) {
10+
end = maxPosition;
11+
steps++;
12+
}
13+
}
14+
return steps;
15+
}
16+
}

0 commit comments

Comments
 (0)