Skip to content

Commit e238b77

Browse files
committed
feat: add solutions to lc problem: No.0788
No.0788.Rotated Digits
1 parent 01faba5 commit e238b77

File tree

6 files changed

+339
-2
lines changed

6 files changed

+339
-2
lines changed

solution/0700-0799/0788.Rotated Digits/README.md

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,145 @@
3535

3636
<!-- 这里可写通用的实现逻辑 -->
3737

38+
**方法一:直接枚举**
39+
40+
一种直观且有效的思路是,直接枚举 $[1, n]$ 中的每个数,判断其是否为好数,若为好数,则答案加一。
41+
42+
那么题目的重点转化为如何判断一个数字 $x$ 是否为好数。判断的逻辑如下:
43+
44+
我们先用哈希表 $d$ 记录每个有效数字对应的旋转数字,在这道题中,有效数字有 `0, 1, 8, 2, 5, 6, 9`,分别对应旋转数字 `0, 1, 8, 5, 2, 9, 6`
45+
46+
然后遍历数字 $x$ 的每一位数字 $v$,如果 $v$ 不在哈希表 $d$ 中,则说明 $x$ 不是好数,直接返回 `false`。否则,将数字 $v$ 对应的旋转数字 $d[v]$ 加入到 $y$ 中。最后,判断 $x$ 和 $y$ 是否相等,若不相等,则说明 $x$ 是好数,返回 `true`
47+
48+
时间复杂度 $O(n\times \log n)$。
49+
3850
<!-- tabs:start -->
3951

4052
### **Python3**
4153

4254
<!-- 这里可写当前语言的特殊实现逻辑 -->
4355

4456
```python
45-
57+
class Solution:
58+
def rotatedDigits(self, n: int) -> int:
59+
def check(x):
60+
y, t = 0, x
61+
k = 1
62+
while t:
63+
v = t % 10
64+
if v not in d:
65+
return False
66+
y = d[v] * k + y
67+
k *= 10
68+
t //= 10
69+
return x != y
70+
71+
d = {0: 0, 1: 1, 8: 8, 2: 5, 5: 2, 6: 9, 9: 6}
72+
return sum(check(i) for i in range(1, n + 1))
4673
```
4774

4875
### **Java**
4976

5077
<!-- 这里可写当前语言的特殊实现逻辑 -->
5178

5279
```java
80+
class Solution {
81+
private static final Map<Integer, Integer> d = new HashMap<>();
82+
static {
83+
d.put(0, 0);
84+
d.put(1, 1);
85+
d.put(8, 8);
86+
d.put(2, 5);
87+
d.put(5, 2);
88+
d.put(6, 9);
89+
d.put(9, 6);
90+
}
91+
92+
public int rotatedDigits(int n) {
93+
int ans = 0;
94+
for (int i = 1; i <= n; ++i) {
95+
if (check(i)) {
96+
++ans;
97+
}
98+
}
99+
return ans;
100+
}
101+
102+
private boolean check(int x) {
103+
int y = 0, t = x;
104+
int k = 1;
105+
while (t > 0) {
106+
int v = t % 10;
107+
if (!d.containsKey(v)) {
108+
return false;
109+
}
110+
y = d.get(v) * k + y;
111+
k *= 10;
112+
t /= 10;
113+
}
114+
return x != y;
115+
}
116+
}
117+
```
118+
119+
### **C++**
120+
121+
```cpp
122+
class Solution {
123+
public:
124+
unordered_map<int, int> d{{0, 0}, {1, 1}, {8, 8}, {2, 5}, {5, 2}, {6, 9}, {9, 6}};
125+
126+
int rotatedDigits(int n) {
127+
int ans = 0;
128+
for (int i = 1; i <= n; ++i) {
129+
ans += check(i);
130+
}
131+
return ans;
132+
}
133+
134+
bool check(int x) {
135+
int y = 0, t = x;
136+
int k = 1;
137+
while (t) {
138+
int v = t % 10;
139+
if (!d.count(v)) {
140+
return false;
141+
}
142+
y = d[v] * k + y;
143+
k *= 10;
144+
t /= 10;
145+
}
146+
return x != y;
147+
}
148+
};
149+
```
53150
151+
### **Go**
152+
153+
```go
154+
func rotatedDigits(n int) int {
155+
d := map[int]int{0: 0, 1: 1, 8: 8, 2: 5, 5: 2, 6: 9, 9: 6}
156+
check := func(x int) bool {
157+
y, t := 0, x
158+
k := 1
159+
for ; t > 0; t /= 10 {
160+
v := t % 10
161+
if _, ok := d[v]; !ok {
162+
return false
163+
}
164+
y = d[v]*k + y
165+
k *= 10
166+
}
167+
return x != y
168+
}
169+
ans := 0
170+
for i := 1; i <= n; i++ {
171+
if check(i) {
172+
ans++
173+
}
174+
}
175+
return ans
176+
}
54177
```
55178

56179
### **...**

solution/0700-0799/0788.Rotated Digits/README_EN.md

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,124 @@ Note that 1 and 10 are not good numbers, since they remain unchanged after rotat
5555
### **Python3**
5656

5757
```python
58-
58+
class Solution:
59+
def rotatedDigits(self, n: int) -> int:
60+
def check(x):
61+
y, t = 0, x
62+
k = 1
63+
while t:
64+
v = t % 10
65+
if v not in d:
66+
return False
67+
y = d[v] * k + y
68+
k *= 10
69+
t //= 10
70+
return x != y
71+
72+
d = {0: 0, 1: 1, 8: 8, 2: 5, 5: 2, 6: 9, 9: 6}
73+
return sum(check(i) for i in range(1, n + 1))
5974
```
6075

6176
### **Java**
6277

6378
```java
79+
class Solution {
80+
private static final Map<Integer, Integer> d = new HashMap<>();
81+
static {
82+
d.put(0, 0);
83+
d.put(1, 1);
84+
d.put(8, 8);
85+
d.put(2, 5);
86+
d.put(5, 2);
87+
d.put(6, 9);
88+
d.put(9, 6);
89+
}
90+
91+
public int rotatedDigits(int n) {
92+
int ans = 0;
93+
for (int i = 1; i <= n; ++i) {
94+
if (check(i)) {
95+
++ans;
96+
}
97+
}
98+
return ans;
99+
}
100+
101+
private boolean check(int x) {
102+
int y = 0, t = x;
103+
int k = 1;
104+
while (t > 0) {
105+
int v = t % 10;
106+
if (!d.containsKey(v)) {
107+
return false;
108+
}
109+
y = d.get(v) * k + y;
110+
k *= 10;
111+
t /= 10;
112+
}
113+
return x != y;
114+
}
115+
}
116+
```
117+
118+
### **C++**
119+
120+
```cpp
121+
class Solution {
122+
public:
123+
unordered_map<int, int> d{{0, 0}, {1, 1}, {8, 8}, {2, 5}, {5, 2}, {6, 9}, {9, 6}};
124+
125+
int rotatedDigits(int n) {
126+
int ans = 0;
127+
for (int i = 1; i <= n; ++i) {
128+
ans += check(i);
129+
}
130+
return ans;
131+
}
132+
133+
bool check(int x) {
134+
int y = 0, t = x;
135+
int k = 1;
136+
while (t) {
137+
int v = t % 10;
138+
if (!d.count(v)) {
139+
return false;
140+
}
141+
y = d[v] * k + y;
142+
k *= 10;
143+
t /= 10;
144+
}
145+
return x != y;
146+
}
147+
};
148+
```
64149
150+
### **Go**
151+
152+
```go
153+
func rotatedDigits(n int) int {
154+
d := map[int]int{0: 0, 1: 1, 8: 8, 2: 5, 5: 2, 6: 9, 9: 6}
155+
check := func(x int) bool {
156+
y, t := 0, x
157+
k := 1
158+
for ; t > 0; t /= 10 {
159+
v := t % 10
160+
if _, ok := d[v]; !ok {
161+
return false
162+
}
163+
y = d[v]*k + y
164+
k *= 10
165+
}
166+
return x != y
167+
}
168+
ans := 0
169+
for i := 1; i <= n; i++ {
170+
if check(i) {
171+
ans++
172+
}
173+
}
174+
return ans
175+
}
65176
```
66177

67178
### **...**
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
unordered_map<int, int> d{{0, 0}, {1, 1}, {8, 8}, {2, 5}, {5, 2}, {6, 9}, {9, 6}};
4+
5+
int rotatedDigits(int n) {
6+
int ans = 0;
7+
for (int i = 1; i <= n; ++i) {
8+
ans += check(i);
9+
}
10+
return ans;
11+
}
12+
13+
bool check(int x) {
14+
int y = 0, t = x;
15+
int k = 1;
16+
while (t) {
17+
int v = t % 10;
18+
if (!d.count(v)) {
19+
return false;
20+
}
21+
y = d[v] * k + y;
22+
k *= 10;
23+
t /= 10;
24+
}
25+
return x != y;
26+
}
27+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func rotatedDigits(n int) int {
2+
d := map[int]int{0: 0, 1: 1, 8: 8, 2: 5, 5: 2, 6: 9, 9: 6}
3+
check := func(x int) bool {
4+
y, t := 0, x
5+
k := 1
6+
for ; t > 0; t /= 10 {
7+
v := t % 10
8+
if _, ok := d[v]; !ok {
9+
return false
10+
}
11+
y = d[v]*k + y
12+
k *= 10
13+
}
14+
return x != y
15+
}
16+
ans := 0
17+
for i := 1; i <= n; i++ {
18+
if check(i) {
19+
ans++
20+
}
21+
}
22+
return ans
23+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
private static final Map<Integer, Integer> d = new HashMap<>();
3+
static {
4+
d.put(0, 0);
5+
d.put(1, 1);
6+
d.put(8, 8);
7+
d.put(2, 5);
8+
d.put(5, 2);
9+
d.put(6, 9);
10+
d.put(9, 6);
11+
}
12+
13+
public int rotatedDigits(int n) {
14+
int ans = 0;
15+
for (int i = 1; i <= n; ++i) {
16+
if (check(i)) {
17+
++ans;
18+
}
19+
}
20+
return ans;
21+
}
22+
23+
private boolean check(int x) {
24+
int y = 0, t = x;
25+
int k = 1;
26+
while (t > 0) {
27+
int v = t % 10;
28+
if (!d.containsKey(v)) {
29+
return false;
30+
}
31+
y = d.get(v) * k + y;
32+
k *= 10;
33+
t /= 10;
34+
}
35+
return x != y;
36+
}
37+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def rotatedDigits(self, n: int) -> int:
3+
def check(x):
4+
y, t = 0, x
5+
k = 1
6+
while t:
7+
v = t % 10
8+
if v not in d:
9+
return False
10+
y = d[v] * k + y
11+
k *= 10
12+
t //= 10
13+
return x != y
14+
15+
d = {0: 0, 1: 1, 8: 8, 2: 5, 5: 2, 6: 9, 9: 6}
16+
return sum(check(i) for i in range(1, n + 1))

0 commit comments

Comments
 (0)