diff --git a/leetcode/2701-2800/2729.Check-if-The-Number-is-Fascinating/README.md b/leetcode/2701-2800/2729.Check-if-The-Number-is-Fascinating/README.md new file mode 100644 index 000000000..8fb3f6c03 --- /dev/null +++ b/leetcode/2701-2800/2729.Check-if-The-Number-is-Fascinating/README.md @@ -0,0 +1,35 @@ +# [2729.Check if The Number is Fascinating][title] + +## Description +You are given an integer `n` that consists of exactly `3` digits. + +We call the number `n` **fascinating** if, after the following modification, the resulting number contains all the digits from `1` to `9` **exactly** once and does not contain any `0`'s: + +- **Concatenate** `n` with the numbers `2 * n` and `3 * n`. + +Return `true` if `n` is fascinating, or `false` otherwise. + +**Concatenating** two numbers means joining them together. For example, the concatenation of `121` and `371` is `121371`. + +**Example 1:** + +``` +Input: n = 192 +Output: true +Explanation: We concatenate the numbers n = 192 and 2 * n = 384 and 3 * n = 576. The resulting number is 192384576. This number contains all the digits from 1 to 9 exactly once. +``` + +**Example 2:** + +``` +Input: n = 100 +Output: false +Explanation: We concatenate the numbers n = 100 and 2 * n = 200 and 3 * n = 300. The resulting number is 100200300. This number does not satisfy any of the conditions. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/check-if-the-number-is-fascinating +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2701-2800/2729.Check-if-The-Number-is-Fascinating/Solution.go b/leetcode/2701-2800/2729.Check-if-The-Number-is-Fascinating/Solution.go index d115ccf5e..d9b50855a 100755 --- a/leetcode/2701-2800/2729.Check-if-The-Number-is-Fascinating/Solution.go +++ b/leetcode/2701-2800/2729.Check-if-The-Number-is-Fascinating/Solution.go @@ -1,5 +1,25 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(n int) bool { + checker := [10]int{} + var checkNum func(n int) + checkNum = func(n int) { + for n > 0 { + x := n % 10 + checker[x]++ + n /= 10 + } + } + checkNum(n) + checkNum(n + n) + checkNum(n + n + n) + if checker[0] > 0 { + return false + } + for i := 1; i < 10; i++ { + if checker[i] != 1 { + return false + } + } + return true } diff --git a/leetcode/2701-2800/2729.Check-if-The-Number-is-Fascinating/Solution_test.go b/leetcode/2701-2800/2729.Check-if-The-Number-is-Fascinating/Solution_test.go index 14ff50eb4..ac8d4b22b 100755 --- a/leetcode/2701-2800/2729.Check-if-The-Number-is-Fascinating/Solution_test.go +++ b/leetcode/2701-2800/2729.Check-if-The-Number-is-Fascinating/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool + inputs int expect bool }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 192, true}, + {"TestCase2", 100, false}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }