Skip to content

Commit 20e624a

Browse files
committed
Add solution and test-cases for problem 1015
1 parent f03226b commit 20e624a

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

leetcode/1001-1100/1015.Smallest-Integer-Divisible-by-K/README.md

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
# [1015.Smallest Integer Divisible by K][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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`.
5+
6+
Return the **length** of `n`. If there is no such `n`, return -1.
7+
8+
**Note**: `n` may not fit in a 64-bit signed integer.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
13+
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.
14+
15+
Return the length of n. If there is no such n, return -1.
1416
15-
## 题意
16-
> ...
17+
Note: n may not fit in a 64-bit signed integer.
18+
```
1719

18-
## 题解
20+
**Example 2:**
1921

20-
### 思路1
21-
> ...
22-
Smallest Integer Divisible by K
23-
```go
2422
```
23+
Input: k = 2
24+
Output: -1
25+
Explanation: There is no such positive integer n divisible by 2.
26+
```
27+
28+
**Example 3:**
2529

30+
```
31+
Input: k = 3
32+
Output: 3
33+
Explanation: The smallest answer is n = 111, which has length 3.
34+
```
2635

2736
## 结语
2837

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(k int) int {
4+
base := 0
5+
// 1, 11, 111,
6+
for i := 1; i <= k; i++ {
7+
base = (base*10 + 1) % k
8+
if base == 0 {
9+
return i
10+
}
11+
}
12+
return -1
513
}

leetcode/1001-1100/1015.Smallest-Integer-Divisible-by-K/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 1, 1},
17+
{"TestCase2", 2, -1},
18+
{"TestCase3", 3, 3},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)