Skip to content

Commit 3b2b06b

Browse files
committed
✨feat: add 788
1 parent 90f0445 commit 3b2b06b

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

Index/模拟.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
| [748. 最短补全词](https://leetcode-cn.com/problems/shortest-completing-word/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/shortest-completing-word/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-j-x4ao/) | 简单 | 🤩🤩🤩🤩 |
101101
| [762. 二进制表示中质数个计算置位](https://leetcode-cn.com/problems/prime-number-of-set-bits-in-binary-representation/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/prime-number-of-set-bits-in-binary-representation/solution/by-ac_oier-w50x/) | 简单 | 🤩🤩🤩🤩 |
102102
| [766. 托普利茨矩阵](https://leetcode-cn.com/problems/toeplitz-matrix/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/toeplitz-matrix/solution/cong-ci-pan-du-qu-cheng-ben-fen-xi-liang-f20w/) | 简单 | 🤩🤩🤩 |
103+
| [788. 旋转数字](https://leetcode.cn/problems/rotated-digits/) | [LeetCode 题解链接](https://leetcode.cn/problems/rotated-digits/solution/by-ac_oier-9qpw/) | 中等 | 🤩🤩🤩🤩 |
103104
| [794. 有效的井字游戏](https://leetcode-cn.com/problems/valid-tic-tac-toe-state/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/valid-tic-tac-toe-state/solution/gong-shui-san-xie-fen-qing-kuang-tao-lun-pikn/) | 中等 | 🤩🤩🤩🤩 |
104105
| [796. 旋转字符串](https://leetcode-cn.com/problems/rotate-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/rotate-string/solution/by-ac_oier-bnkx/) | 简单 | 🤩🤩🤩 |
105106
| [804. 唯一摩尔斯密码词](https://leetcode-cn.com/problems/unique-morse-code-words/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/unique-morse-code-words/solution/by-ac_oier-a9hv/) | 简单 | 🤩🤩🤩 |
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[788. 旋转数字](https://leetcode.cn/problems/rotated-digits/solution/by-ac_oier-9qpw/)** ,难度为 **中等**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
我们称一个数 `X` 为好数, 如果它的每位数字逐个地被旋转 `180` 度后,我们仍可以得到一个有效的,且和 `X` 不同的数。要求每位数字都要被旋转。
10+
11+
如果一个数的每位数字被旋转以后仍然还是一个数字, 则这个数是有效的。0, 1, 和 8 被旋转后仍然是它们自己;2 和 5 可以互相旋转成对方(在这种情况下,它们以不同的方向旋转,换句话说,2 和 5 互为镜像);6 和 9 同理,除了这些以外其他的数字旋转以后都不再是有效的数字。
12+
13+
现在我们有一个正整数 `N`, 计算从 `1` 到 `N` 中有多少个数 X 是好数?
14+
15+
示例:
16+
```
17+
输入: 10
18+
19+
输出: 4
20+
21+
解释:
22+
在[1, 10]中有四个好数: 2, 5, 6, 9。
23+
注意 1 和 10 不是好数, 因为他们在旋转之后不变。
24+
```
25+
26+
提示:
27+
* `N` 的取值范围是 $[1, 10000]$。
28+
29+
---
30+
31+
### 模拟
32+
33+
利用 $n$ 的范围为 $1e4$,我们可以直接检查 $[1, n]$ 的每个数。
34+
35+
由于每一个位数都需要翻转,因此如果当前枚举到的数值 `x` 中包含非有效翻转数字(非 `0125689`)则必然不是好数;而在每一位均为有效数字的前提下,若当前枚举到的数值 `x` 中包含翻转后能够发生数值上变化的数值(`2569`),则为好数。
36+
37+
Java 代码:
38+
```Java
39+
class Solution {
40+
public int rotatedDigits(int n) {
41+
int ans = 0;
42+
out:for (int i = 1; i <= n; i++) {
43+
boolean ok = false;
44+
int x = i;
45+
while (x != 0) {
46+
int t = x % 10;
47+
x /= 10;
48+
if (t == 2 || t == 5 || t == 6 || t == 9) ok = true;
49+
else if (t != 0 && t != 1 && t != 8) continue out;
50+
}
51+
if (ok) ans++;
52+
}
53+
return ans;
54+
}
55+
}
56+
```
57+
TypeScript 代码:
58+
```TypeScript
59+
function rotatedDigits(n: number): number {
60+
let ans = 0
61+
out:for (let i = 1; i <= n; i++) {
62+
let ok = false
63+
let x = i
64+
while (x != 0) {
65+
const t = x % 10
66+
x = Math.floor(x / 10)
67+
if (t == 2 || t == 5 || t == 6 || t == 9) ok = true
68+
else if (t != 0 && t != 1 && t != 8) continue out
69+
}
70+
if (ok) ans++
71+
}
72+
return ans
73+
};
74+
```
75+
Python3 代码:
76+
```Python3
77+
class Solution:
78+
def rotatedDigits(self, n: int) -> int:
79+
ans = 0
80+
for i in range(1, n + 1):
81+
ok, x = False, i
82+
while x != 0:
83+
t = x % 10
84+
x = x // 10
85+
if t == 2 or t == 5 or t == 6 or t == 9:
86+
ok = True
87+
elif t != 0 and t != 1 and t != 8:
88+
ok = False
89+
break
90+
ans = ans + 1 if ok else ans
91+
return ans
92+
```
93+
* 时间复杂度:共有 $n$ 个数需要枚举,检查一个数需要遍历其每个数字,复杂度为 $O(\log{n})$。整体复杂度为 $O(n\log{n})$
94+
* 空间复杂度:$O(1)$
95+
96+
---
97+
98+
### 最后
99+
100+
这是我们「刷穿 LeetCode」系列文章的第 `No.788` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
101+
102+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
103+
104+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
105+
106+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
107+

0 commit comments

Comments
 (0)