Skip to content

Add solution and test-cases for problem 788 #851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions leetcode/701-800/0788.Rotated-Digits/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
# [788.Rotated Digits][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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.

A number is valid if each digit remains a digit after rotation. For example:

- `0`, `1`, and `8` rotate to themselves,
- `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),
- `6` and `9` rotate to each other, and
- the rest of the numbers do not rotate to any other number and become invalid.

Given an integer `n`, return the number of **good** integers in the range `[1, n]`.

**Example 1:**

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

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Rotated Digits
```go
```
Input: n = 1
Output: 0
```

**Example 3:**

```
Input: n = 2
Output: 1
```

## 结语

Expand Down
30 changes: 28 additions & 2 deletions leetcode/701-800/0788.Rotated-Digits/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
package Solution

func Solution(x bool) bool {
return x
var (
rotateMap = map[int]int{
0: 0, 1: 1, 8: 8,
2: 5, 5: 2, 6: 9, 9: 6,
}
)

func isNumOk(n int) bool {
ans := false
for n > 0 {
x := n % 10
v, ok := rotateMap[x]
if !ok {
return false
}
ans = ans || x != v
n /= 10
}
return ans
}
func Solution(n int) int {
ans := 0
for i := 1; i <= n; i++ {
if isNumOk(i) {
ans++
}
}
return ans
}
14 changes: 7 additions & 7 deletions leetcode/701-800/0788.Rotated-Digits/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 10, 4},
{"TestCase2", 1, 0},
{"TestCase3", 2, 1},
}

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

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

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