Skip to content

Commit 653268a

Browse files
authored
Merge pull request #1107 from 0xff-dev/728
Add solution and test-cases for problem 728
2 parents c32d1b6 + 294f306 commit 653268a

File tree

3 files changed

+46
-27
lines changed

3 files changed

+46
-27
lines changed

leetcode/701-800/0728.Self-Dividing-Numbers/README.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
# [728.Self Dividing Numbers][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+
A **self-dividing number** is a number that is divisible by every digit it contains.
5+
6+
- For example, `128` is a **self-dividing number** because `128 % 1 == 0`, `128 % 2 == 0`, and `128 % 8 == 0`.
7+
8+
A **self-dividing number** is not allowed to contain the digit zero.
9+
10+
Given two integers `left` and `right`, return a list of all the **self-dividing numbers** in the range `[left, right]` (both **inclusive**).
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: left = 1, right = 22
16+
Output: [1,2,3,4,5,6,7,8,9,11,12,15,22]
1317
```
1418

15-
## 题意
16-
> ...
19+
**Example 2:**
1720

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Self Dividing Numbers
23-
```go
2421
```
25-
22+
Input: left = 47, right = 85
23+
Output: [48,55,66,77]
24+
```
2625

2726
## 结语
2827

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(left int, right int) []int {
4+
var res []int
5+
var ok func(int) bool
6+
ok = func(n int) bool {
7+
self := n
8+
for n > 0 {
9+
mod := n % 10
10+
if mod == 0 {
11+
return false
12+
}
13+
if self%mod != 0 {
14+
return false
15+
}
16+
n /= 10
17+
}
18+
return true
19+
}
20+
for i := left; i <= right; i++ {
21+
if ok(i) {
22+
res = append(res, i)
23+
}
24+
}
25+
return res
526
}

leetcode/701-800/0728.Self-Dividing-Numbers/Solution_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
left, right int
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 1, 22, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22}},
17+
{"TestCase2", 47, 85, []int{48, 55, 66, 77}},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.left, c.right)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.left, c.right)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)