Skip to content

Commit 674dfc5

Browse files
committed
feat: add solutions to lc problem: No.1088
No.1088.Confusing Number II
1 parent 5e8c2fc commit 674dfc5

File tree

7 files changed

+471
-2
lines changed

7 files changed

+471
-2
lines changed

solution/1000-1099/1088.Confusing Number II/README.md

+175-1
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,196 @@
5959

6060
<!-- 这里可写通用的实现逻辑 -->
6161

62+
**方法一:数位 DP**
63+
64+
我们先将数字 $n$ 转成字符串 $s$。
65+
66+
接下来,我们定义一个函数 $check(x)$,用来判断 $x$ 在旋转 $180^\circ$ 之后是否变成了一个不同的数。如果 $x$ 在旋转 $180^\circ$ 之后变成了一个不同的数,那么我们就称 $x$ 是一个易混淆数。
67+
68+
然后,我们定义另一个函数 $dfs(pos, limit, x)$,用于搜索从高位到低位的每一位。其中:
69+
70+
- 参数 $pos$ 表示当前搜索到的位置,初始时为 $0$;
71+
- 参数 $limit$ 表示当前搜索的数是否受到上界的限制,初始时为 $true$;
72+
- 参数 $x$ 表示当前搜索的数,初始时为 $0$。
73+
74+
在 $dfs(pos, limit, x)$ 中,如果 $pos \geq len(s)$,那么我们就判断 $x$ 是否是一个易混淆数,如果是则返回 $1$,否则返回 $0$。
75+
76+
否则,我们计算出当前位置上的数字的上界 $up$,然后枚举当前位置上的数字 $i$,如果 $i$ 在旋转 $180^\circ$ 之后不是一个数字,那么我们就直接跳过这个数字。否则,我们将 $x$ 更新为 $x \times 10 + i$,并根据 $limit$ 的值决定下一步搜索的时候是否受到上界的限制,最后将答案返回。
77+
78+
最终的答案即为 $dfs(0, true, 0)$。
79+
80+
时间复杂度 $O(5^{\log_{10}n})$,空间复杂度 $O(\log_{10}n)$。其中 $5^{\log_{10}n}$ 表示 $n$ 的位数。
81+
6282
<!-- tabs:start -->
6383

6484
### **Python3**
6585

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

6888
```python
69-
89+
class Solution:
90+
def confusingNumberII(self, n: int) -> int:
91+
def check(x: int) -> bool:
92+
y, t = 0, x
93+
while t:
94+
t, v = divmod(t, 10)
95+
y = y * 10 + d[v]
96+
return x != y
97+
98+
def dfs(pos: int, limit: bool, x: int) -> int:
99+
if pos >= len(s):
100+
return int(check(x))
101+
up = int(s[pos]) if limit else 9
102+
ans = 0
103+
for i in range(up + 1):
104+
if d[i] != -1:
105+
ans += dfs(pos + 1, limit and i == up, x * 10 + i)
106+
return ans
107+
108+
d = [0, 1, -1, -1, -1, -1, 9, -1, 8, 6]
109+
s = str(n)
110+
return dfs(0, True, 0)
70111
```
71112

72113
### **Java**
73114

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

76117
```java
118+
class Solution {
119+
private final int[] d = {0, 1, -1, -1, -1, -1, 9, -1, 8, 6};
120+
private String s;
121+
122+
public int confusingNumberII(int n) {
123+
s = String.valueOf(n);
124+
return dfs(0, 1, 0);
125+
}
126+
127+
private int dfs(int pos, int limit, int x) {
128+
if (pos >= s.length()) {
129+
return check(x) ? 1 : 0;
130+
}
131+
int up = limit == 1 ? s.charAt(pos) - '0' : 9;
132+
int ans = 0;
133+
for (int i = 0; i <= up; ++i) {
134+
if (d[i] != -1) {
135+
ans += dfs(pos + 1, limit == 1 && i == up ? 1 : 0, x * 10 + i);
136+
}
137+
}
138+
return ans;
139+
}
140+
141+
private boolean check(int x) {
142+
int y = 0;
143+
for (int t = x; t > 0; t /= 10) {
144+
int v = t % 10;
145+
y = y * 10 + d[v];
146+
}
147+
return x != y;
148+
}
149+
}
150+
```
151+
152+
### **C++**
153+
154+
```cpp
155+
class Solution {
156+
public:
157+
int confusingNumberII(int n) {
158+
string s = to_string(n);
159+
int d[10] = {0, 1, -1, -1, -1, -1, 9, -1, 8, 6};
160+
auto check = [&](int x) -> bool {
161+
int y = 0;
162+
for (int t = x; t; t /= 10) {
163+
int v = t % 10;
164+
y = y * 10 + d[v];
165+
}
166+
return x != y;
167+
};
168+
function<int(int, int, int)> dfs = [&](int pos, int limit, int x) -> int {
169+
if (pos >= s.size()) {
170+
return check(x);
171+
}
172+
int up = limit ? s[pos] - '0' : 9;
173+
int ans = 0;
174+
for (int i = 0; i <= up; ++i) {
175+
if (d[i] != -1) {
176+
ans += dfs(pos + 1, limit && i == up, x * 10 + i);
177+
}
178+
}
179+
return ans;
180+
};
181+
return dfs(0, 1, 0);
182+
}
183+
};
184+
```
185+
186+
### **Go**
187+
188+
```go
189+
func confusingNumberII(n int) int {
190+
d := [10]int{0, 1, -1, -1, -1, -1, 9, -1, 8, 6}
191+
s := strconv.Itoa(n)
192+
check := func(x int) bool {
193+
y := 0
194+
for t := x; t > 0; t /= 10 {
195+
v := t % 10
196+
y = y*10 + d[v]
197+
}
198+
return x != y
199+
}
200+
var dfs func(pos int, limit bool, x int) int
201+
dfs = func(pos int, limit bool, x int) (ans int) {
202+
if pos >= len(s) {
203+
if check(x) {
204+
return 1
205+
}
206+
return 0
207+
}
208+
up := 9
209+
if limit {
210+
up = int(s[pos] - '0')
211+
}
212+
for i := 0; i <= up; i++ {
213+
if d[i] != -1 {
214+
ans += dfs(pos+1, limit && i == up, x*10+i)
215+
}
216+
}
217+
return
218+
}
219+
return dfs(0, true, 0)
220+
}
221+
```
77222

223+
### **TypeScript**
224+
225+
```ts
226+
function confusingNumberII(n: number): number {
227+
const s = n.toString();
228+
const d: number[] = [0, 1, -1, -1, -1, -1, 9, -1, 8, 6];
229+
const check = (x: number) => {
230+
let y = 0;
231+
for (let t = x; t > 0; t = Math.floor(t / 10)) {
232+
const v = t % 10;
233+
y = y * 10 + d[v];
234+
}
235+
return x !== y;
236+
};
237+
const dfs = (pos: number, limit: boolean, x: number): number => {
238+
if (pos >= s.length) {
239+
return check(x) ? 1 : 0;
240+
}
241+
const up = limit ? parseInt(s[pos]) : 9;
242+
let ans = 0;
243+
for (let i = 0; i <= up; ++i) {
244+
if (d[i] !== -1) {
245+
ans += dfs(pos + 1, limit && i === up, x * 10 + i);
246+
}
247+
}
248+
return ans;
249+
};
250+
return dfs(0, true, 0);
251+
}
78252
```
79253

80254
### **...**

solution/1000-1099/1088.Confusing Number II/README_EN.md

+155-1
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,167 @@
5858
### **Python3**
5959

6060
```python
61-
61+
class Solution:
62+
def confusingNumberII(self, n: int) -> int:
63+
def check(x: int) -> bool:
64+
y, t = 0, x
65+
while t:
66+
t, v = divmod(t, 10)
67+
y = y * 10 + d[v]
68+
return x != y
69+
70+
def dfs(pos: int, limit: bool, x: int) -> int:
71+
if pos >= len(s):
72+
return int(check(x))
73+
up = int(s[pos]) if limit else 9
74+
ans = 0
75+
for i in range(up + 1):
76+
if d[i] != -1:
77+
ans += dfs(pos + 1, limit and i == up, x * 10 + i)
78+
return ans
79+
80+
d = [0, 1, -1, -1, -1, -1, 9, -1, 8, 6]
81+
s = str(n)
82+
return dfs(0, True, 0)
6283
```
6384

6485
### **Java**
6586

6687
```java
88+
class Solution {
89+
private final int[] d = {0, 1, -1, -1, -1, -1, 9, -1, 8, 6};
90+
private String s;
91+
92+
public int confusingNumberII(int n) {
93+
s = String.valueOf(n);
94+
return dfs(0, 1, 0);
95+
}
96+
97+
private int dfs(int pos, int limit, int x) {
98+
if (pos >= s.length()) {
99+
return check(x) ? 1 : 0;
100+
}
101+
int up = limit == 1 ? s.charAt(pos) - '0' : 9;
102+
int ans = 0;
103+
for (int i = 0; i <= up; ++i) {
104+
if (d[i] != -1) {
105+
ans += dfs(pos + 1, limit == 1 && i == up ? 1 : 0, x * 10 + i);
106+
}
107+
}
108+
return ans;
109+
}
110+
111+
private boolean check(int x) {
112+
int y = 0;
113+
for (int t = x; t > 0; t /= 10) {
114+
int v = t % 10;
115+
y = y * 10 + d[v];
116+
}
117+
return x != y;
118+
}
119+
}
120+
```
121+
122+
### **C++**
123+
124+
```cpp
125+
class Solution {
126+
public:
127+
int confusingNumberII(int n) {
128+
string s = to_string(n);
129+
int d[10] = {0, 1, -1, -1, -1, -1, 9, -1, 8, 6};
130+
auto check = [&](int x) -> bool {
131+
int y = 0;
132+
for (int t = x; t; t /= 10) {
133+
int v = t % 10;
134+
y = y * 10 + d[v];
135+
}
136+
return x != y;
137+
};
138+
function<int(int, int, int)> dfs = [&](int pos, int limit, int x) -> int {
139+
if (pos >= s.size()) {
140+
return check(x);
141+
}
142+
int up = limit ? s[pos] - '0' : 9;
143+
int ans = 0;
144+
for (int i = 0; i <= up; ++i) {
145+
if (d[i] != -1) {
146+
ans += dfs(pos + 1, limit && i == up, x * 10 + i);
147+
}
148+
}
149+
return ans;
150+
};
151+
return dfs(0, 1, 0);
152+
}
153+
};
154+
```
155+
156+
### **Go**
157+
158+
```go
159+
func confusingNumberII(n int) int {
160+
d := [10]int{0, 1, -1, -1, -1, -1, 9, -1, 8, 6}
161+
s := strconv.Itoa(n)
162+
check := func(x int) bool {
163+
y := 0
164+
for t := x; t > 0; t /= 10 {
165+
v := t % 10
166+
y = y*10 + d[v]
167+
}
168+
return x != y
169+
}
170+
var dfs func(pos int, limit bool, x int) int
171+
dfs = func(pos int, limit bool, x int) (ans int) {
172+
if pos >= len(s) {
173+
if check(x) {
174+
return 1
175+
}
176+
return 0
177+
}
178+
up := 9
179+
if limit {
180+
up = int(s[pos] - '0')
181+
}
182+
for i := 0; i <= up; i++ {
183+
if d[i] != -1 {
184+
ans += dfs(pos+1, limit && i == up, x*10+i)
185+
}
186+
}
187+
return
188+
}
189+
return dfs(0, true, 0)
190+
}
191+
```
67192

193+
### **TypeScript**
194+
195+
```ts
196+
function confusingNumberII(n: number): number {
197+
const s = n.toString();
198+
const d: number[] = [0, 1, -1, -1, -1, -1, 9, -1, 8, 6];
199+
const check = (x: number) => {
200+
let y = 0;
201+
for (let t = x; t > 0; t = Math.floor(t / 10)) {
202+
const v = t % 10;
203+
y = y * 10 + d[v];
204+
}
205+
return x !== y;
206+
};
207+
const dfs = (pos: number, limit: boolean, x: number): number => {
208+
if (pos >= s.length) {
209+
return check(x) ? 1 : 0;
210+
}
211+
const up = limit ? parseInt(s[pos]) : 9;
212+
let ans = 0;
213+
for (let i = 0; i <= up; ++i) {
214+
if (d[i] !== -1) {
215+
ans += dfs(pos + 1, limit && i === up, x * 10 + i);
216+
}
217+
}
218+
return ans;
219+
};
220+
return dfs(0, true, 0);
221+
}
68222
```
69223

70224
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
int confusingNumberII(int n) {
4+
string s = to_string(n);
5+
int d[10] = {0, 1, -1, -1, -1, -1, 9, -1, 8, 6};
6+
auto check = [&](int x) -> bool {
7+
int y = 0;
8+
for (int t = x; t; t /= 10) {
9+
int v = t % 10;
10+
y = y * 10 + d[v];
11+
}
12+
return x != y;
13+
};
14+
function<int(int, int, int)> dfs = [&](int pos, int limit, int x) -> int {
15+
if (pos >= s.size()) {
16+
return check(x);
17+
}
18+
int up = limit ? s[pos] - '0' : 9;
19+
int ans = 0;
20+
for (int i = 0; i <= up; ++i) {
21+
if (d[i] != -1) {
22+
ans += dfs(pos + 1, limit && i == up, x * 10 + i);
23+
}
24+
}
25+
return ans;
26+
};
27+
return dfs(0, 1, 0);
28+
}
29+
};

0 commit comments

Comments
 (0)