diff --git a/problems/0045.跳跃游戏II.md b/problems/0045.跳跃游戏II.md index 911834e1..9b13d31d 100644 --- a/problems/0045.跳跃游戏II.md +++ b/problems/0045.跳跃游戏II.md @@ -386,13 +386,13 @@ impl Solution { let mut cur_distance = 0; let mut ans = 0; let mut next_distance = 0; - for (n, &i) in nums.iter().enumerate() { - next_distance = (n as i32 + i).max(next_distance); + for (i, &n) in nums.iter().enumerate().take(nums.len() - 1) { + next_distance = (n as usize + i).max(next_distance); if i == cur_distance { - if cur_distance < n as i32 - 1 { + if cur_distance < nums.len() - 1 { ans += 1; cur_distance = next_distance; - if next_distance >= n as i32 - 1 { + if next_distance >= nums.len() - 1 { break; }; } else { @@ -403,7 +403,6 @@ impl Solution { ans } } - ``` ```Rust @@ -416,8 +415,8 @@ impl Solution { let mut cur_distance = 0; let mut ans = 0; let mut next_distance = 0; - for (n, &i) in nums.iter().enumerate() { - next_distance = (n as i32 + i).max(next_distance); + for (i, &n) in nums.iter().enumerate().take(nums.len() - 1) { + next_distance = (n as usize + i).max(next_distance); if i == cur_distance { cur_distance = next_distance; ans += 1; @@ -426,7 +425,6 @@ impl Solution { ans } } - ```