From ae39e59255a56e8ba4285a0b5b843716c28d1317 Mon Sep 17 00:00:00 2001 From: cezarbbb <105843128+cezarbbb@users.noreply.github.com> Date: Thu, 28 Jul 2022 17:45:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200045.=E8=B7=B3=E8=B7=83?= =?UTF-8?q?=E6=B8=B8=E6=88=8FII=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0045.跳跃游戏II Rust版本 --- problems/0045.跳跃游戏II.md | 50 +++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/problems/0045.跳跃游戏II.md b/problems/0045.跳跃游戏II.md index 648355fd..13142c99 100644 --- a/problems/0045.跳跃游戏II.md +++ b/problems/0045.跳跃游戏II.md @@ -305,6 +305,56 @@ object Solution { } ``` +### Rust + +```Rust +//版本一 +impl Solution { + fn max(a: i32, b:i32) -> i32 { + if a > b { a } else { b } + } + pub fn jump(nums: Vec) -> i32 { + if nums.len() == 0 { return 0; } + let mut cur_distance: i32 = 0; + let mut ans: i32 = 0; + let mut next_distance: i32 = 0; + for i in 0..nums.len() { + next_distance = Self::max(nums[i] + i as i32, next_distance); + if i as i32 == cur_distance { + if cur_distance != (nums.len() - 1) as i32 { + ans += 1; + cur_distance = next_distance; + if next_distance == (nums.len() - 1) as i32 { break; } + } + else { break; } + } + } + ans + } +} +``` + +```Rust +//版本二 +impl Solution { + fn max(a: i32, b:i32) -> i32 { + if a > b { a } else { b } + } + pub fn jump(nums: Vec) -> i32 { + let mut cur_distance: i32 = 0; + let mut ans: i32 = 0; + let mut next_distance: i32 = 0; + for i in 0..nums.len() - 1 { + next_distance = Self::max(nums[i] + i as i32, next_distance); + if i as i32 == cur_distance { + cur_distance = next_distance; + ans += 1; + } + } + ans + } +} +``` -----------------------