Skip to content

Commit 9aa18cc

Browse files
committed
feat: update solutions to lc problem: No.0788
No.0788.Rotated Digits
1 parent 95ad570 commit 9aa18cc

File tree

4 files changed

+45
-90
lines changed

4 files changed

+45
-90
lines changed

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

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -37,42 +37,42 @@
3737

3838
**方法一:直接枚举**
3939

40-
一种直观且有效的思路是,直接枚举 $[1, n]$ 中的每个数,判断其是否为好数,若为好数,则答案加一。
40+
一种直观且有效的思路是,直接枚举 $[1,2,..n]$ 中的每个数,判断其是否为好数,若为好数,则答案加一。
4141

4242
那么题目的重点转化为如何判断一个数字 $x$ 是否为好数。判断的逻辑如下:
4343

44-
我们先用哈希表 $d$ 记录每个有效数字对应的旋转数字,在这道题中,有效数字有 `0, 1, 8, 2, 5, 6, 9`,分别对应旋转数字 `0, 1, 8, 5, 2, 9, 6`
44+
我们先用一个长度为 $10$ 的数组 $d$ 记录每个有效数字对应的旋转数字,在这道题中,有效数字有 $[0, 1, 8, 2, 5, 6, 9]$,分别对应旋转数字 $[0, 1, 8, 5, 2, 9, 6]$。如果不是有效数字,我们将对应的旋转数字设为 $-1$
4545

46-
然后遍历数字 $x$ 的每一位数字 $v$,如果 $v$ 不在哈希表 $d$ 中,则说明 $x$ 不是好数,直接返回 `false`。否则,将数字 $v$ 对应的旋转数字 $d[v]$ 加入到 $y$ 中。最后,判断 $x$ 和 $y$ 是否相等,若不相等,则说明 $x$ 是好数,返回 `true`
46+
然后遍历数字 $x$ 的每一位数字 $v$,如果 $v$ 不是有效数字,说明 $x$ 不是好数,直接返回 `false`。否则,我们将数字 $v$ 对应的旋转数字 $d[v]$ 加入到 $y$ 中。最后,判断 $x$ 和 $y$ 是否相等,若不相等,则说明 $x$ 是好数,返回 `true`
4747

4848
时间复杂度 $O(n\times \log n)$。
4949

5050
**方法二:数位 DP**
5151

5252
方法一的做法足以通过本题,但时间复杂度较高。如果题目的数据范围达到 $10^9$ 级别,则方法一的做法会超出时间限制。
5353

54-
这道题实际上是求在给定区间 $[l, r]$ 中,满足条件的数的个数。条件与数的大小无关,而只与数的组成有关,因此可以使用数位 DP 的思想求解。数位 DP 中,数的大小对复杂度的影响很小。
54+
这道题实际上是求在给定区间 $[l,..r]$ 中,满足条件的数的个数。条件与数的大小无关,而只与数的组成有关,因此可以使用数位 DP 的思想求解。数位 DP 中,数的大小对复杂度的影响很小。
5555

56-
对于区间 $[l, r]$ 问题,我们一般会将其转化为 $[1, r]$ 问题,然后再减去 $[1, l - 1]$ 问题,即:
56+
对于区间 $[l,..r]$ 问题,我们一般会将其转化为 $[1,..r]$ 然后再减去 $[1,..l - 1]$ 的问题,即:
5757

5858
$$
5959
ans = \sum_{i=1}^{r} ans_i - \sum_{i=1}^{l-1} ans_i
6060
$$
6161

62-
不过对于本题而言,我们只需要求出区间 $[1, r]$ 的值即可。
62+
不过对于本题而言,我们只需要求出区间 $[1,..r]$ 的值即可。
6363

6464
这里我们用记忆化搜索来实现数位 DP。从起点向下搜索,到最底层得到方案数,一层层向上返回答案并累加,最后从搜索起点得到最终的答案。
6565

6666
基本步骤如下:
6767

68-
1. 将数字 $x$ 转为 int 数组 $a$,其中 $a[1]$ 为最低位,而 $a[len]$ 为最高位;
68+
1. 将数字 $n$ 转为 int 数组 $a$,其中 $a[1]$ 为最低位,而 $a[len]$ 为最高位;
6969
1. 根据题目信息,设计函数 $dfs()$,对于本题,我们定义 $dfs(pos, ok, limit)$,答案为 $dfs(len, 0, true)$。
7070

7171
其中:
7272

7373
- `pos` 表示数字的位数,从末位或者第一位开始,一般根据题目的数字构造性质来选择顺序。对于本题,我们选择从高位开始,因此,`pos` 的初始值为 `len`
74-
- `ok` 表示当前数字是否满足题目要求(对于本题,如果数字出现 `2, 5, 6, 9` 则满足)
75-
- `limit` 表示可填的数字的限制,如果无限制,那么可以选择 $[0, 9]$,否则,只能选择 $[0, a[pos]]$。如果 `limit``true` 且已经取到了能取到的最大值,那么下一个 `limit` 同样为 `true`;如果 `limit``true` 但是还没有取到最大值,或者 `limit``false`,那么下一个 `limit``false`
74+
- `ok` 表示当前数字是否满足题目要求(对于本题,如果数字出现 $[2, 5, 6, 9]$ 则满足)
75+
- `limit` 表示可填的数字的限制,如果无限制,那么可以选择 $[0,1,..9]$,否则,只能选择 $[0,..a[pos]]$。如果 `limit``true` 且已经取到了能取到的最大值,那么下一个 `limit` 同样为 `true`;如果 `limit``true` 但是还没有取到最大值,或者 `limit``false`,那么下一个 `limit``false`
7676

7777
关于函数的实现细节,可以参考下面的代码。
7878

@@ -92,14 +92,14 @@ class Solution:
9292
k = 1
9393
while t:
9494
v = t % 10
95-
if v not in d:
95+
if d[v] == -1:
9696
return False
9797
y = d[v] * k + y
9898
k *= 10
9999
t //= 10
100100
return x != y
101101

102-
d = {0: 0, 1: 1, 8: 8, 2: 5, 5: 2, 6: 9, 9: 6}
102+
d = [0, 1, 5, -1, -1, 2, 9, -1, 8, 6]
103103
return sum(check(i) for i in range(1, n + 1))
104104
```
105105

@@ -134,16 +134,7 @@ class Solution:
134134

135135
```java
136136
class Solution {
137-
private static final Map<Integer, Integer> d = new HashMap<>();
138-
static {
139-
d.put(0, 0);
140-
d.put(1, 1);
141-
d.put(8, 8);
142-
d.put(2, 5);
143-
d.put(5, 2);
144-
d.put(6, 9);
145-
d.put(9, 6);
146-
}
137+
private int[] d = new int[] {0, 1, 5, -1, -1, 2, 9, -1, 8, 6};
147138

148139
public int rotatedDigits(int n) {
149140
int ans = 0;
@@ -160,10 +151,10 @@ class Solution {
160151
int k = 1;
161152
while (t > 0) {
162153
int v = t % 10;
163-
if (!d.containsKey(v)) {
154+
if (d[v] == -1) {
164155
return false;
165156
}
166-
y = d.get(v) * k + y;
157+
y = d[v] * k + y;
167158
k *= 10;
168159
t /= 10;
169160
}
@@ -178,17 +169,13 @@ class Solution {
178169
private int[][] dp = new int[6][2];
179170

180171
public int rotatedDigits(int n) {
181-
return f(n);
182-
}
183-
184-
private int f(int x) {
185172
int len = 0;
186173
for (var e : dp) {
187174
Arrays.fill(e, -1);
188175
}
189-
while (x > 0) {
190-
a[++len] = x % 10;
191-
x /= 10;
176+
while (n > 0) {
177+
a[++len] = n % 10;
178+
n /= 10;
192179
}
193180
return dfs(len, 0, true);
194181
}
@@ -223,7 +210,7 @@ class Solution {
223210
```cpp
224211
class Solution {
225212
public:
226-
unordered_map<int, int> d{{0, 0}, {1, 1}, {8, 8}, {2, 5}, {5, 2}, {6, 9}, {9, 6}};
213+
const vector<int> d = {0, 1, 5, -1, -1, 2, 9, -1, 8, 6};
227214

228215
int rotatedDigits(int n) {
229216
int ans = 0;
@@ -238,7 +225,7 @@ public:
238225
int k = 1;
239226
while (t) {
240227
int v = t % 10;
241-
if (!d.count(v)) {
228+
if (d[v] == -1) {
242229
return false;
243230
}
244231
y = d[v] * k + y;
@@ -257,15 +244,11 @@ public:
257244
int dp[6][2];
258245
259246
int rotatedDigits(int n) {
260-
return f(n);
261-
}
262-
263-
int f(int x) {
264247
memset(dp, -1, sizeof dp);
265248
int len = 0;
266-
while (x) {
267-
a[++len] = x % 10;
268-
x /= 10;
249+
while (n) {
250+
a[++len] = n % 10;
251+
n /= 10;
269252
}
270253
return dfs(len, 0, true);
271254
}
@@ -292,21 +275,20 @@ public:
292275
}
293276
return ans;
294277
}
295-
296278
};
297279
```
298280

299281
### **Go**
300282

301283
```go
302284
func rotatedDigits(n int) int {
303-
d := map[int]int{0: 0, 1: 1, 8: 8, 2: 5, 5: 2, 6: 9, 9: 6}
285+
d := []int{0, 1, 5, -1, -1, 2, 9, -1, 8, 6}
304286
check := func(x int) bool {
305287
y, t := 0, x
306288
k := 1
307289
for ; t > 0; t /= 10 {
308290
v := t % 10
309-
if _, ok := d[v]; !ok {
291+
if d[v] == -1 {
310292
return false
311293
}
312294
y = d[v]*k + y

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

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ class Solution:
6262
k = 1
6363
while t:
6464
v = t % 10
65-
if v not in d:
65+
if d[v] == -1:
6666
return False
6767
y = d[v] * k + y
6868
k *= 10
6969
t //= 10
7070
return x != y
7171

72-
d = {0: 0, 1: 1, 8: 8, 2: 5, 5: 2, 6: 9, 9: 6}
72+
d = [0, 1, 5, -1, -1, 2, 9, -1, 8, 6]
7373
return sum(check(i) for i in range(1, n + 1))
7474
```
7575

@@ -102,16 +102,7 @@ class Solution:
102102

103103
```java
104104
class Solution {
105-
private static final Map<Integer, Integer> d = new HashMap<>();
106-
static {
107-
d.put(0, 0);
108-
d.put(1, 1);
109-
d.put(8, 8);
110-
d.put(2, 5);
111-
d.put(5, 2);
112-
d.put(6, 9);
113-
d.put(9, 6);
114-
}
105+
private int[] d = new int[] {0, 1, 5, -1, -1, 2, 9, -1, 8, 6};
115106

116107
public int rotatedDigits(int n) {
117108
int ans = 0;
@@ -128,10 +119,10 @@ class Solution {
128119
int k = 1;
129120
while (t > 0) {
130121
int v = t % 10;
131-
if (!d.containsKey(v)) {
122+
if (d[v] == -1) {
132123
return false;
133124
}
134-
y = d.get(v) * k + y;
125+
y = d[v] * k + y;
135126
k *= 10;
136127
t /= 10;
137128
}
@@ -146,17 +137,13 @@ class Solution {
146137
private int[][] dp = new int[6][2];
147138

148139
public int rotatedDigits(int n) {
149-
return f(n);
150-
}
151-
152-
private int f(int x) {
153140
int len = 0;
154141
for (var e : dp) {
155142
Arrays.fill(e, -1);
156143
}
157-
while (x > 0) {
158-
a[++len] = x % 10;
159-
x /= 10;
144+
while (n > 0) {
145+
a[++len] = n % 10;
146+
n /= 10;
160147
}
161148
return dfs(len, 0, true);
162149
}
@@ -191,7 +178,7 @@ class Solution {
191178
```cpp
192179
class Solution {
193180
public:
194-
unordered_map<int, int> d{{0, 0}, {1, 1}, {8, 8}, {2, 5}, {5, 2}, {6, 9}, {9, 6}};
181+
const vector<int> d = {0, 1, 5, -1, -1, 2, 9, -1, 8, 6};
195182

196183
int rotatedDigits(int n) {
197184
int ans = 0;
@@ -206,7 +193,7 @@ public:
206193
int k = 1;
207194
while (t) {
208195
int v = t % 10;
209-
if (!d.count(v)) {
196+
if (d[v] == -1) {
210197
return false;
211198
}
212199
y = d[v] * k + y;
@@ -225,15 +212,11 @@ public:
225212
int dp[6][2];
226213
227214
int rotatedDigits(int n) {
228-
return f(n);
229-
}
230-
231-
int f(int x) {
232215
memset(dp, -1, sizeof dp);
233216
int len = 0;
234-
while (x) {
235-
a[++len] = x % 10;
236-
x /= 10;
217+
while (n) {
218+
a[++len] = n % 10;
219+
n /= 10;
237220
}
238221
return dfs(len, 0, true);
239222
}
@@ -260,21 +243,20 @@ public:
260243
}
261244
return ans;
262245
}
263-
264246
};
265247
```
266248

267249
### **Go**
268250

269251
```go
270252
func rotatedDigits(n int) int {
271-
d := map[int]int{0: 0, 1: 1, 8: 8, 2: 5, 5: 2, 6: 9, 9: 6}
253+
d := []int{0, 1, 5, -1, -1, 2, 9, -1, 8, 6}
272254
check := func(x int) bool {
273255
y, t := 0, x
274256
k := 1
275257
for ; t > 0; t /= 10 {
276258
v := t % 10
277-
if _, ok := d[v]; !ok {
259+
if d[v] == -1 {
278260
return false
279261
}
280262
y = d[v]*k + y

solution/0700-0799/0788.Rotated Digits/Solution.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@ class Solution {
44
int dp[6][2];
55

66
int rotatedDigits(int n) {
7-
return f(n);
8-
}
9-
10-
int f(int x) {
117
memset(dp, -1, sizeof dp);
128
int len = 0;
13-
while (x) {
14-
a[++len] = x % 10;
15-
x /= 10;
9+
while (n) {
10+
a[++len] = n % 10;
11+
n /= 10;
1612
}
1713
return dfs(len, 0, true);
1814
}
@@ -39,5 +35,4 @@ class Solution {
3935
}
4036
return ans;
4137
}
42-
4338
};

solution/0700-0799/0788.Rotated Digits/Solution.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@ class Solution {
33
private int[][] dp = new int[6][2];
44

55
public int rotatedDigits(int n) {
6-
return f(n);
7-
}
8-
9-
private int f(int x) {
106
int len = 0;
117
for (var e : dp) {
128
Arrays.fill(e, -1);
139
}
14-
while (x > 0) {
15-
a[++len] = x % 10;
16-
x /= 10;
10+
while (n > 0) {
11+
a[++len] = n % 10;
12+
n /= 10;
1713
}
1814
return dfs(len, 0, true);
1915
}

0 commit comments

Comments
 (0)