Skip to content

Commit 2b62da3

Browse files
authored
Merge pull request #851 from 0xff-dev/788
Add solution and test-cases for problem 788
2 parents 022b1b0 + 2cd6163 commit 2b62da3

File tree

3 files changed

+59
-22
lines changed

3 files changed

+59
-22
lines changed

leetcode/701-800/0788.Rotated-Digits/README.md

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
# [788.Rotated 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+
An integer `x` is a **good** if after rotating each digit individually by 180 degrees, we get a valid number that is different from `x`. Each digit must be rotated - we cannot choose to leave it alone.
5+
6+
A number is valid if each digit remains a digit after rotation. For example:
7+
8+
- `0`, `1`, and `8` rotate to themselves,
9+
- `2` and `5` rotate to each other (in this case they are rotated in a different direction, in other words, `2` or `5` gets mirrored),
10+
- `6` and `9` rotate to each other, and
11+
- the rest of the numbers do not rotate to any other number and become invalid.
12+
13+
Given an integer `n`, return the number of **good** integers in the range `[1, n]`.
714

815
**Example 1:**
916

1017
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
18+
Input: n = 10
19+
Output: 4
20+
Explanation: There are four good numbers in the range [1, 10] : 2, 5, 6, 9.
21+
Note that 1 and 10 are not good numbers, since they remain unchanged after rotating.
1322
```
1423

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

20-
### 思路1
21-
> ...
22-
Rotated Digits
23-
```go
2426
```
27+
Input: n = 1
28+
Output: 0
29+
```
30+
31+
**Example 3:**
2532

33+
```
34+
Input: n = 2
35+
Output: 1
36+
```
2637

2738
## 结语
2839

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

3-
func Solution(x bool) bool {
4-
return x
3+
var (
4+
rotateMap = map[int]int{
5+
0: 0, 1: 1, 8: 8,
6+
2: 5, 5: 2, 6: 9, 9: 6,
7+
}
8+
)
9+
10+
func isNumOk(n int) bool {
11+
ans := false
12+
for n > 0 {
13+
x := n % 10
14+
v, ok := rotateMap[x]
15+
if !ok {
16+
return false
17+
}
18+
ans = ans || x != v
19+
n /= 10
20+
}
21+
return ans
22+
}
23+
func Solution(n int) int {
24+
ans := 0
25+
for i := 1; i <= n; i++ {
26+
if isNumOk(i) {
27+
ans++
28+
}
29+
}
30+
return ans
531
}

leetcode/701-800/0788.Rotated-Digits/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 int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 10, 4},
17+
{"TestCase2", 1, 0},
18+
{"TestCase3", 2, 1},
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)