diff --git a/leetcode/701-800/0728.Self-Dividing-Numbers/README.md b/leetcode/701-800/0728.Self-Dividing-Numbers/README.md index 1bbbeb534..bca516c3c 100644 --- a/leetcode/701-800/0728.Self-Dividing-Numbers/README.md +++ b/leetcode/701-800/0728.Self-Dividing-Numbers/README.md @@ -1,28 +1,27 @@ # [728.Self Dividing Numbers][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 +A **self-dividing number** is a number that is divisible by every digit it contains. + +- For example, `128` is a **self-dividing number** because `128 % 1 == 0`, `128 % 2 == 0`, and `128 % 8 == 0`. + +A **self-dividing number** is not allowed to contain the digit zero. + +Given two integers `left` and `right`, return a list of all the **self-dividing numbers** in the range `[left, right]` (both **inclusive**). **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: left = 1, right = 22 +Output: [1,2,3,4,5,6,7,8,9,11,12,15,22] ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Self Dividing Numbers -```go ``` - +Input: left = 47, right = 85 +Output: [48,55,66,77] +``` ## 结语 diff --git a/leetcode/701-800/0728.Self-Dividing-Numbers/Solution.go b/leetcode/701-800/0728.Self-Dividing-Numbers/Solution.go index d115ccf5e..c89d3bda9 100644 --- a/leetcode/701-800/0728.Self-Dividing-Numbers/Solution.go +++ b/leetcode/701-800/0728.Self-Dividing-Numbers/Solution.go @@ -1,5 +1,26 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(left int, right int) []int { + var res []int + var ok func(int) bool + ok = func(n int) bool { + self := n + for n > 0 { + mod := n % 10 + if mod == 0 { + return false + } + if self%mod != 0 { + return false + } + n /= 10 + } + return true + } + for i := left; i <= right; i++ { + if ok(i) { + res = append(res, i) + } + } + return res } diff --git a/leetcode/701-800/0728.Self-Dividing-Numbers/Solution_test.go b/leetcode/701-800/0728.Self-Dividing-Numbers/Solution_test.go index 14ff50eb4..151983453 100644 --- a/leetcode/701-800/0728.Self-Dividing-Numbers/Solution_test.go +++ b/leetcode/701-800/0728.Self-Dividing-Numbers/Solution_test.go @@ -9,31 +9,30 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + left, right int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 1, 22, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22}}, + {"TestCase2", 47, 85, []int{48, 55, 66, 77}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.left, c.right) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.left, c.right) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }