From 05028415306e7678ef1121e7c93dd6611897d68e Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Tue, 14 Mar 2023 11:58:01 +0800 Subject: [PATCH] =?UTF-8?q?Update=200045.=E8=B7=B3=E8=B7=83=E6=B8=B8?= =?UTF-8?q?=E6=88=8FII.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0045.跳跃游戏II.md | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) 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 } } - ```