diff --git a/leetcode/701-800/0788.Rotated-Digits/README.md b/leetcode/701-800/0788.Rotated-Digits/README.md index 41ff7f445..7f99d77d3 100644 --- a/leetcode/701-800/0788.Rotated-Digits/README.md +++ b/leetcode/701-800/0788.Rotated-Digits/README.md @@ -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 +``` ## 结语 diff --git a/leetcode/701-800/0788.Rotated-Digits/Solution.go b/leetcode/701-800/0788.Rotated-Digits/Solution.go index d115ccf5e..87c51c4ba 100644 --- a/leetcode/701-800/0788.Rotated-Digits/Solution.go +++ b/leetcode/701-800/0788.Rotated-Digits/Solution.go @@ -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 } diff --git a/leetcode/701-800/0788.Rotated-Digits/Solution_test.go b/leetcode/701-800/0788.Rotated-Digits/Solution_test.go index 14ff50eb4..11331ef26 100644 --- a/leetcode/701-800/0788.Rotated-Digits/Solution_test.go +++ b/leetcode/701-800/0788.Rotated-Digits/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }