From f3420fbcf1ef4bdab35a89d3a8f64be2820da472 Mon Sep 17 00:00:00 2001 From: xiaolatiao <1628652790@qq.com> Date: Sun, 9 Jul 2023 20:21:39 +0800 Subject: [PATCH] feat: add rust solution to lc problem: No.2582 Signed-off-by: xiaolatiao <1628652790@qq.com> --- .../2500-2599/2582.Pass the Pillow/README.md | 36 +++++++++++++++++++ .../2582.Pass the Pillow/README_EN.md | 36 +++++++++++++++++++ .../2582.Pass the Pillow/Solution.rs | 12 +++++++ 3 files changed, 84 insertions(+) create mode 100644 solution/2500-2599/2582.Pass the Pillow/Solution.rs diff --git a/solution/2500-2599/2582.Pass the Pillow/README.md b/solution/2500-2599/2582.Pass the Pillow/README.md index ae21c45520c50..68916fec49ea8 100644 --- a/solution/2500-2599/2582.Pass the Pillow/README.md +++ b/solution/2500-2599/2582.Pass the Pillow/README.md @@ -197,6 +197,42 @@ function passThePillow(n: number, time: number): number { } ``` +### **Rust** + +```rust +impl Solution { + pub fn pass_the_pillow(n: i32, time: i32) -> i32 { + let mut ans = 1; + let mut k = 1; + + for i in 1..=time { + ans += k; + + if ans == 1 || ans == n { + k *= -1; + } + } + + ans + } +} +``` + +```rust +impl Solution { + pub fn pass_the_pillow(n: i32, time: i32) -> i32 { + let mut k = time / (n - 1); + let mut _mod = time % (n - 1); + + if k & 1 == 1 { + return n - _mod + } + + _mod + 1 + } +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2582.Pass the Pillow/README_EN.md b/solution/2500-2599/2582.Pass the Pillow/README_EN.md index c3912cbb33256..59a041a684c6b 100644 --- a/solution/2500-2599/2582.Pass the Pillow/README_EN.md +++ b/solution/2500-2599/2582.Pass the Pillow/README_EN.md @@ -184,6 +184,42 @@ function passThePillow(n: number, time: number): number { } ``` +### **Rust** + +```rust +impl Solution { + pub fn pass_the_pillow(n: i32, time: i32) -> i32 { + let mut ans = 1; + let mut k = 1; + + for i in 1..=time { + ans += k; + + if ans == 1 || ans == n { + k *= -1; + } + } + + ans + } +} +``` + +```rust +impl Solution { + pub fn pass_the_pillow(n: i32, time: i32) -> i32 { + let mut k = time / (n - 1); + let mut _mod = time % (n - 1); + + if k & 1 == 1 { + return n - _mod + } + + _mod + 1 + } +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2582.Pass the Pillow/Solution.rs b/solution/2500-2599/2582.Pass the Pillow/Solution.rs new file mode 100644 index 0000000000000..0a0a222434bfd --- /dev/null +++ b/solution/2500-2599/2582.Pass the Pillow/Solution.rs @@ -0,0 +1,12 @@ +impl Solution { + pub fn pass_the_pillow(n: i32, time: i32) -> i32 { + let mut k = time / (n - 1); + let mut _mod = time % (n - 1); + + if k & 1 == 1 { + return n - _mod + } + + _mod + 1 + } +} \ No newline at end of file