Update 0045.跳跃游戏II.md
This commit is contained in:
parent
2b73437e72
commit
0502841530
|
|
@ -386,13 +386,13 @@ impl Solution {
|
||||||
let mut cur_distance = 0;
|
let mut cur_distance = 0;
|
||||||
let mut ans = 0;
|
let mut ans = 0;
|
||||||
let mut next_distance = 0;
|
let mut next_distance = 0;
|
||||||
for (n, &i) in nums.iter().enumerate() {
|
for (i, &n) in nums.iter().enumerate().take(nums.len() - 1) {
|
||||||
next_distance = (n as i32 + i).max(next_distance);
|
next_distance = (n as usize + i).max(next_distance);
|
||||||
if i == cur_distance {
|
if i == cur_distance {
|
||||||
if cur_distance < n as i32 - 1 {
|
if cur_distance < nums.len() - 1 {
|
||||||
ans += 1;
|
ans += 1;
|
||||||
cur_distance = next_distance;
|
cur_distance = next_distance;
|
||||||
if next_distance >= n as i32 - 1 {
|
if next_distance >= nums.len() - 1 {
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -403,7 +403,6 @@ impl Solution {
|
||||||
ans
|
ans
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```Rust
|
```Rust
|
||||||
|
|
@ -416,8 +415,8 @@ impl Solution {
|
||||||
let mut cur_distance = 0;
|
let mut cur_distance = 0;
|
||||||
let mut ans = 0;
|
let mut ans = 0;
|
||||||
let mut next_distance = 0;
|
let mut next_distance = 0;
|
||||||
for (n, &i) in nums.iter().enumerate() {
|
for (i, &n) in nums.iter().enumerate().take(nums.len() - 1) {
|
||||||
next_distance = (n as i32 + i).max(next_distance);
|
next_distance = (n as usize + i).max(next_distance);
|
||||||
if i == cur_distance {
|
if i == cur_distance {
|
||||||
cur_distance = next_distance;
|
cur_distance = next_distance;
|
||||||
ans += 1;
|
ans += 1;
|
||||||
|
|
@ -426,7 +425,6 @@ impl Solution {
|
||||||
ans
|
ans
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue