Skip to content

Commit

Permalink
Merge pull request #1040 from 0xff-dev/2729
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 2729
  • Loading branch information
6boris authored Dec 24, 2024
2 parents 634f12f + 2747dda commit 6af411c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit 6af411c

Please sign in to comment.