diff --git a/leetcode/1001-1100/1015.Smallest-Integer-Divisible-by-K/README.md b/leetcode/1001-1100/1015.Smallest-Integer-Divisible-by-K/README.md index 3d8420b45..50d344a76 100644 --- a/leetcode/1001-1100/1015.Smallest-Integer-Divisible-by-K/README.md +++ b/leetcode/1001-1100/1015.Smallest-Integer-Divisible-by-K/README.md @@ -1,28 +1,37 @@ # [1015.Smallest Integer Divisible by K][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +Given a positive integer `k`, you need to find the **length** of the **smallest** positive integer `n` such that `n` is divisible by `k`, and `n` only contains the digit `1`. + +Return the **length** of `n`. If there is no such `n`, return -1. + +**Note**: `n` may not fit in a 64-bit signed integer. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" -``` +Given a positive integer k, you need to find the length of the smallest positive integer n such that n is divisible by k, and n only contains the digit 1. + +Return the length of n. If there is no such n, return -1. -## 题意 -> ... +Note: n may not fit in a 64-bit signed integer. +``` -## 题解 +**Example 2:** -### 思路1 -> ... -Smallest Integer Divisible by K -```go ``` +Input: k = 2 +Output: -1 +Explanation: There is no such positive integer n divisible by 2. +``` + +**Example 3:** +``` +Input: k = 3 +Output: 3 +Explanation: The smallest answer is n = 111, which has length 3. +``` ## 结语 diff --git a/leetcode/1001-1100/1015.Smallest-Integer-Divisible-by-K/Solution.go b/leetcode/1001-1100/1015.Smallest-Integer-Divisible-by-K/Solution.go index d115ccf5e..9f97c2bbb 100644 --- a/leetcode/1001-1100/1015.Smallest-Integer-Divisible-by-K/Solution.go +++ b/leetcode/1001-1100/1015.Smallest-Integer-Divisible-by-K/Solution.go @@ -1,5 +1,13 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(k int) int { + base := 0 + // 1, 11, 111, + for i := 1; i <= k; i++ { + base = (base*10 + 1) % k + if base == 0 { + return i + } + } + return -1 } diff --git a/leetcode/1001-1100/1015.Smallest-Integer-Divisible-by-K/Solution_test.go b/leetcode/1001-1100/1015.Smallest-Integer-Divisible-by-K/Solution_test.go index 14ff50eb4..df051e8cb 100644 --- a/leetcode/1001-1100/1015.Smallest-Integer-Divisible-by-K/Solution_test.go +++ b/leetcode/1001-1100/1015.Smallest-Integer-Divisible-by-K/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 1, 1}, + {"TestCase2", 2, -1}, + {"TestCase3", 3, 3}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }