Skip to content

Commit 2747dda

Browse files
committed
Add solution and test-cases for problem 2729
1 parent dbd4dda commit 2747dda

File tree

3 files changed

+62
-8
lines changed

3 files changed

+62
-8
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# [2729.Check if The Number is Fascinating][title]
2+
3+
## Description
4+
You are given an integer `n` that consists of exactly `3` digits.
5+
6+
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:
7+
8+
- **Concatenate** `n` with the numbers `2 * n` and `3 * n`.
9+
10+
Return `true` if `n` is fascinating, or `false` otherwise.
11+
12+
**Concatenating** two numbers means joining them together. For example, the concatenation of `121` and `371` is `121371`.
13+
14+
**Example 1:**
15+
16+
```
17+
Input: n = 192
18+
Output: true
19+
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.
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Input: n = 100
26+
Output: false
27+
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.
28+
```
29+
30+
## 结语
31+
32+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
33+
34+
[title]: https://leetcode.com/problems/check-if-the-number-is-fascinating
35+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int) bool {
4+
checker := [10]int{}
5+
var checkNum func(n int)
6+
checkNum = func(n int) {
7+
for n > 0 {
8+
x := n % 10
9+
checker[x]++
10+
n /= 10
11+
}
12+
}
13+
checkNum(n)
14+
checkNum(n + n)
15+
checkNum(n + n + n)
16+
if checker[0] > 0 {
17+
return false
18+
}
19+
for i := 1; i < 10; i++ {
20+
if checker[i] != 1 {
21+
return false
22+
}
23+
}
24+
return true
525
}

leetcode/2701-2800/2729.Check-if-The-Number-is-Fascinating/Solution_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs int
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 192, true},
17+
{"TestCase2", 100, false},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

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

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

0 commit comments

Comments
 (0)