From 158cc18e57a8d36c5538838e152d93e8bd7d62fe Mon Sep 17 00:00:00 2001 From: Xu Shaohua Date: Fri, 31 May 2024 07:44:08 +0800 Subject: [PATCH] leetcode: Add 0055 --- src/leetcode/0055.jump-game/Cargo.toml | 7 +++ src/leetcode/0055.jump-game/index.md | 4 ++ src/leetcode/0055.jump-game/src/main.rs | 82 +++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 src/leetcode/0055.jump-game/Cargo.toml create mode 100644 src/leetcode/0055.jump-game/index.md create mode 100644 src/leetcode/0055.jump-game/src/main.rs diff --git a/src/leetcode/0055.jump-game/Cargo.toml b/src/leetcode/0055.jump-game/Cargo.toml new file mode 100644 index 000000000..981dc805d --- /dev/null +++ b/src/leetcode/0055.jump-game/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "lc-0055-jump-game" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] diff --git a/src/leetcode/0055.jump-game/index.md b/src/leetcode/0055.jump-game/index.md new file mode 100644 index 000000000..77a618ba8 --- /dev/null +++ b/src/leetcode/0055.jump-game/index.md @@ -0,0 +1,4 @@ + +# + +[问题描述](../problems/) diff --git a/src/leetcode/0055.jump-game/src/main.rs b/src/leetcode/0055.jump-game/src/main.rs new file mode 100644 index 000000000..3b990b79b --- /dev/null +++ b/src/leetcode/0055.jump-game/src/main.rs @@ -0,0 +1,82 @@ +// Copyright (c) 2024 Xu Shaohua . All rights reserved. +// Use of this source is governed by General Public License that can be found +// in the LICENSE file. + +// Greedy +pub fn can_jump1(nums: Vec) -> bool { + debug_assert!((1..=10_000).contains(&nums.len())); + + // 最远跳跃距离 + let mut max_pos: usize = 0; + + for (index, &num) in nums.iter().enumerate() { + let num_usize: usize = num as usize; + + // max_pos >= index, 表示能从之前的节点跳到这里. + // index + num_usize > max_pos, 表示基于当前的点, 可以跳得更远. + if max_pos >= index && index + num_usize > max_pos { + // 更新最远跳跃距离 + max_pos = index + num_usize; + } + } + // 是否遍历完数组, 到达最远的位置 + max_pos + 1 >= nums.len() +} + +pub fn can_jump2(nums: Vec) -> bool { + debug_assert!((1..=10_000).contains(&nums.len())); + + // 最远跳跃距离 + let mut max_pos: usize = 0; + + for (index, &num) in nums.iter().enumerate() { + // index > max_pos, 表示能从之前的节点无法跳到这里. + if index > max_pos { + return false; + } + + // index + num_usize > max_pos, 表示基于当前的点, 可以跳得更远. + let num_usize: usize = num as usize; + if max_pos >= index && index + num_usize > max_pos { + // 更新最远跳跃距离. + max_pos = index + num_usize; + + // 如果这次跳得足够远, 超过了数组长度, 就直接提前返回 true. + if max_pos + 1 >= nums.len() { + return true; + } + } + } + + true +} + +pub type SolutionFn = fn(Vec) -> bool; + +fn check_can_jump(func: SolutionFn) { + let nums = vec![2, 3, 1, 1, 4]; + assert!(func(nums)); + + let nums = vec![3, 2, 1, 0, 4]; + assert!(!func(nums)); +} + +fn main() { + check_can_jump(can_jump1); + check_can_jump(can_jump2); +} + +#[cfg(test)] +mod tests { + use super::{can_jump1, can_jump2, check_can_jump}; + + #[test] + fn test_can_jump1() { + check_can_jump(can_jump1); + } + + #[test] + fn test_can_jump2() { + check_can_jump(can_jump2); + } +}