Skip to content

Commit b342e44

Browse files
authored
Merge pull request #897 from 0xff-dev/1922
Add solution and test-cases for problem 1922
2 parents c329102 + 03679ef commit b342e44

File tree

3 files changed

+52
-22
lines changed

3 files changed

+52
-22
lines changed

leetcode/1901-2000/1922.Count-Good-Numbers/README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
# [1922.Count Good Numbers][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+
A digit string is **good** if the digits **(0-indexed)** at **even** indices are **even** and the digits at **odd** indices are **prime** (`2`, `3`, `5`, or `7`).
5+
6+
- For example, `"2582"` is good because the digits (`2` and `8`) at even positions are even and the digits (`5` and `2`) at odd positions are prime. However, `"3245"` is **not** good because `3` is at an even index but is not even.
7+
8+
Given an integer `n`, return the **total** number of good digit strings of length `n`. Since the answer may be large, **return it modulo** `10^9 + 7`.
9+
10+
A **digit string** is a string consisting of digits `0` through `9` that may contain leading zeros.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: n = 1
16+
Output: 5
17+
Explanation: The good numbers of length 1 are "0", "2", "4", "6", "8".
1318
```
1419

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

20-
### 思路1
21-
> ...
22-
Count Good Numbers
23-
```go
2422
```
23+
Input: n = 4
24+
Output: 400
25+
```
26+
27+
**Example 3:**
2528

29+
```
30+
Input: n = 50
31+
Output: 564908303
32+
```
2633

2734
## 结语
2835

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

3-
func Solution(x bool) bool {
4-
return x
3+
const mod1922 = 1000000007
4+
5+
func quickPow(a int, n int64) int {
6+
if n == 0 {
7+
return 1
8+
}
9+
if n == 1 {
10+
return a
11+
}
12+
13+
ans := quickPow(a, n>>1)
14+
ans = (ans * ans) % mod1922
15+
if n&1 == 1 {
16+
ans = (ans * a) % mod1922
17+
}
18+
return ans
19+
}
20+
21+
func Solution(n int64) int {
22+
add := int64(0)
23+
if n&1 == 1 {
24+
add = 1
25+
}
26+
mid := n >> 1
27+
return (quickPow(5, mid+add) * quickPow(4, mid)) % mod1922
528
}

leetcode/1901-2000/1922.Count-Good-Numbers/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs int64
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 1, 5},
17+
{"TestCase2", 4, 400},
18+
{"TestCase3", 50, 564908303},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)