Skip to content

Commit 2323dd8

Browse files
committed
Add solution and test-cases for problem 357
1 parent bdac664 commit 2323dd8

File tree

3 files changed

+37
-21
lines changed

3 files changed

+37
-21
lines changed

leetcode/301-400/0357.Count-Numbers-with-Unique-Digits/README.md

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
# [357.Count Numbers with Unique Digits][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+
Given an integer `n`, return the count of all numbers with unique digits, `x`, where 0 <= x < 10<sup>n</sup>.
5+
76

87
**Example 1:**
98

109
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
10+
Input: n = 2
11+
Output: 91
12+
Explanation: The answer should be the total numbers in the range of 0 ≤ x < 100, excluding 11,22,33,44,55,66,77,88,99
1313
```
1414

15-
## 题意
16-
> ...
17-
18-
## 题解
15+
**Example 2:**
1916

20-
### 思路1
21-
> ...
22-
Count Numbers with Unique Digits
23-
```go
2417
```
25-
18+
Input: n = 0
19+
Output: 1
20+
```
2621

2722
## 结语
2823

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

3-
func Solution(x bool) bool {
4-
return x
3+
/*
4+
5+
1: 10
6+
末尾是0
7+
2: 10 + 1*9 + 8*9 = 91
8+
末尾是0 末尾非0,
9+
3: 91 + 1*9*8 + 9*8*8 = 739
10+
11+
4: 739 + 1*9*8*7 + 9*8*8*7 = 5275
12+
*/
13+
func Solution(n int) int {
14+
ans := make([]int, 9)
15+
ans[0], ans[1] = 1, 10
16+
base1, base2 := 1, 8
17+
for idx := 2; idx <= 8; idx++ {
18+
diff := 11 - idx
19+
base1 *= diff
20+
base2 *= diff
21+
ans[idx] = ans[idx-1] + base1 + base2
22+
}
23+
return ans[n]
524
}

leetcode/301-400/0357.Count-Numbers-with-Unique-Digits/Solution_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 0, 1},
17+
{"TestCase2", 2, 91},
18+
{"TestCase3", 3, 739},
19+
{"TestCase4", 4, 5275},
20+
{"TestCase5", 8, 2345851},
1921
}
2022

2123
// 开始测试

0 commit comments

Comments
 (0)