From e4ec66f7b33e92c0d1990e2589295105f5b57b19 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 25 Nov 2025 06:54:55 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1015 --- .../README.md | 17 +++++++++++++ .../README_EN.md | 25 ++++++++++++++++++- .../Solution.rs | 12 +++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 solution/1000-1099/1015.Smallest Integer Divisible by K/Solution.rs diff --git a/solution/1000-1099/1015.Smallest Integer Divisible by K/README.md b/solution/1000-1099/1015.Smallest Integer Divisible by K/README.md index c89674bb17a59..7fa3c7e2caafc 100644 --- a/solution/1000-1099/1015.Smallest Integer Divisible by K/README.md +++ b/solution/1000-1099/1015.Smallest Integer Divisible by K/README.md @@ -150,6 +150,23 @@ function smallestRepunitDivByK(k: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn smallest_repunit_div_by_k(k: i32) -> i32 { + let mut n = 1 % k; + for i in 1..=k { + if n == 0 { + return i; + } + n = (n * 10 + 1) % k; + } + -1 + } +} +``` + diff --git a/solution/1000-1099/1015.Smallest Integer Divisible by K/README_EN.md b/solution/1000-1099/1015.Smallest Integer Divisible by K/README_EN.md index 007d55d9a1e58..e5a2f652e4e9a 100644 --- a/solution/1000-1099/1015.Smallest Integer Divisible by K/README_EN.md +++ b/solution/1000-1099/1015.Smallest Integer Divisible by K/README_EN.md @@ -63,7 +63,13 @@ tags: -### Solution 1 +### Solution 1: Mathematics + +We observe that the positive integer $n$ starts with an initial value of $1$, and each time it is multiplied by $10$ and then $1$ is added, i.e., $n = n \times 10 + 1$. Since $(n \times 10 + 1) \bmod k = ((n \bmod k) \times 10 + 1) \bmod k$, we can determine whether $n$ is divisible by $k$ by calculating $n \bmod k$. + +We start from $n = 1$ and calculate $n \bmod k$ each time until $n \bmod k = 0$. At this point, $n$ is the smallest positive integer we are looking for, and its length is the number of digits in $n$. Otherwise, we update $n = (n \times 10 + 1) \bmod k$. If after looping $k$ times we still haven't found $n \bmod k = 0$, it means no such $n$ exists, and we return $-1$. + +The time complexity is $O(k)$ and the space complexity is $O(1)$, where $k$ is the given positive integer. @@ -145,6 +151,23 @@ function smallestRepunitDivByK(k: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn smallest_repunit_div_by_k(k: i32) -> i32 { + let mut n = 1 % k; + for i in 1..=k { + if n == 0 { + return i; + } + n = (n * 10 + 1) % k; + } + -1 + } +} +``` + diff --git a/solution/1000-1099/1015.Smallest Integer Divisible by K/Solution.rs b/solution/1000-1099/1015.Smallest Integer Divisible by K/Solution.rs new file mode 100644 index 0000000000000..1d07a1e329203 --- /dev/null +++ b/solution/1000-1099/1015.Smallest Integer Divisible by K/Solution.rs @@ -0,0 +1,12 @@ +impl Solution { + pub fn smallest_repunit_div_by_k(k: i32) -> i32 { + let mut n = 1 % k; + for i in 1..=k { + if n == 0 { + return i; + } + n = (n * 10 + 1) % k; + } + -1 + } +}