You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We design a function $dfs(i, s)$, which represents that we are currently enumerating the number $i$, and there are still numbers with a sum of $s$ to be enumerated. The current search path is $t$, and the answer is $ans$.
62
+
63
+
The execution logic of the function $dfs(i, s)$ is as follows:
64
+
65
+
Approach One:
66
+
67
+
- If $s = 0$ and the length of the current search path $t$ is $k$, it means that a group of answers has been found. Add $t$ to $ans$ and then return.
68
+
- If $i \gt 9$ or $i \gt s$ or the length of the current search path $t$ is greater than $k$, it means that the current search path cannot be the answer, so return directly.
69
+
- Otherwise, we can choose to add the number $i$ to the search path $t$, and then continue to search, i.e., execute $dfs(i + 1, s - i)$. After the search is completed, remove $i$ from the search path $t$; we can also choose not to add the number $i$ to the search path $t$, and directly execute $dfs(i + 1, s)$.
60
70
61
71
<!-- tabs:start -->
62
72
@@ -257,7 +267,15 @@ public class Solution {
257
267
258
268
<!-- tabs:end -->
259
269
260
-
### Solution 2
270
+
Another approach:
271
+
272
+
- If $s = 0$ and the length of the current search path $t$ is $k$, it means that a group of answers has been found. Add $t$ to $ans$ and then return.
273
+
- If $i \gt 9$ or $i \gt s$ or the length of the current search path $t$ is greater than $k$, it means that the current search path cannot be the answer, so return directly.
274
+
- Otherwise, we enumerate the next number $j$, i.e., $j \in [i, 9]$, add the number $j$ to the search path $t$, and then continue to search, i.e., execute $dfs(j + 1, s - j)$. After the search is completed, remove $j$ from the search path $t$.
275
+
276
+
In the main function, we call $dfs(1, n)$, i.e., start enumerating from the number $1$, and the remaining numbers with a sum of $n$ need to be enumerated. After the search is completed, we can get all the answers.
277
+
278
+
The time complexity is $(C_{9}^k \times k)$, and the space complexity is $O(k)$.
261
279
262
280
<!-- tabs:start -->
263
281
@@ -424,7 +442,19 @@ public class Solution {
424
442
425
443
<!-- tabs:end -->
426
444
427
-
### Solution 3
445
+
### Solution 2: Binary Enumeration
446
+
447
+
We can use a binary integer of length $9$ to represent the selection of numbers $1$ to $9$, where the $i$-th bit of the binary integer represents whether the number $i + 1$ is selected. If the $i$-th bit is $1$, it means that the number $i + 1$ is selected, otherwise, it means that the number $i + 1$ is not selected.
448
+
449
+
We enumerate binary integers in the range of $[0, 2^9)$. For the currently enumerated binary integer $mask$, if the number of $1$s in the binary representation of $mask$ is $k$, and the sum of the numbers corresponding to $1$ in the binary representation of $mask$ is $n$, it means that the number selection scheme corresponding to $mask$ is a group of answers. We can add the number selection scheme corresponding to $mask$ to the answer.
450
+
451
+
The time complexity is $O(2^9 \times 9)$, and the space complexity is $O(k)$.
Copy file name to clipboardexpand all lines: solution/0200-0299/0246.Strobogrammatic Number/README_EN.md
+7-1
Original file line number
Diff line number
Diff line change
@@ -43,7 +43,13 @@
43
43
44
44
## Solutions
45
45
46
-
### Solution 1
46
+
### Solution 1: Two Pointers Simulation
47
+
48
+
We define an array $d$, where $d[i]$ represents the number after rotating the digit $i$ by 180°. If $d[i]$ is $-1$, it means that the digit $i$ cannot be rotated 180° to get a valid digit.
49
+
50
+
We define two pointers $i$ and $j$, pointing to the left and right ends of the string, respectively. Then we continuously move the pointers towards the center, checking whether $d[num[i]]$ and $num[j]$ are equal. If they are not equal, it means that the string is not a strobogrammatic number, and we can directly return $false$. If $i > j$, it means that we have traversed the entire string, and we return $true$.
51
+
52
+
The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(1)$.
Copy file name to clipboardexpand all lines: solution/0200-0299/0247.Strobogrammatic Number II/README_EN.md
+19-1
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,25 @@
27
27
28
28
## Solutions
29
29
30
-
### Solution 1
30
+
### Solution 1: Recursion
31
+
32
+
If the length is $1$, then the strobogrammatic numbers are only $0, 1, 8$; if the length is $2$, then the strobogrammatic numbers are only $11, 69, 88, 96$.
33
+
34
+
We design a recursive function $dfs(u)$, which returns the strobogrammatic numbers of length $u$. The answer is $dfs(n)$.
35
+
36
+
If $u$ is $0$, return a list containing an empty string, i.e., `[""]`; if $u$ is $1$, return the list `["0", "1", "8"]`.
37
+
38
+
If $u$ is greater than $1$, we traverse all the strobogrammatic numbers of length $u - 2$. For each strobogrammatic number $v$, we add $1, 8, 6, 9$ to both sides of it, and we can get the strobogrammatic numbers of length `u`.
39
+
40
+
Note that if $u \neq n$, we can also add $0$ to both sides of the strobogrammatic number.
41
+
42
+
Finally, we return all the strobogrammatic numbers of length $n$.
43
+
44
+
The time complexity is $O(2^{n+2})$.
45
+
46
+
Similar problems:
47
+
48
+
-[248. Strobogrammatic Number III 🔒](https://github.com/doocs/leetcode/blob/main/solution/0200-0299/0248.Strobogrammatic%20Number%20III/README_EN.md)
Copy file name to clipboardexpand all lines: solution/0200-0299/0248.Strobogrammatic Number III/README_EN.md
+21-1
Original file line number
Diff line number
Diff line change
@@ -30,7 +30,27 @@
30
30
31
31
## Solutions
32
32
33
-
### Solution 1
33
+
### Solution 1: Recursion
34
+
35
+
If the length is $1$, then the strobogrammatic numbers are only $0, 1, 8$; if the length is $2$, then the strobogrammatic numbers are only $11, 69, 88, 96$.
36
+
37
+
We design a recursive function $dfs(u)$, which returns the strobogrammatic numbers of length $u$.
38
+
39
+
If $u$ is $0$, return a list containing an empty string, i.e., `[""]`; if $u$ is $1$, return the list `["0", "1", "8"]`.
40
+
41
+
If $u$ is greater than $1$, we traverse all the strobogrammatic numbers of length $u - 2$. For each strobogrammatic number $v$, we add $1, 8, 6, 9$ to both sides of it, and we can get the strobogrammatic numbers of length $u$.
42
+
43
+
Note that if $u \neq n$, we can also add $0$ to both sides of the strobogrammatic number.
44
+
45
+
Let the lengths of $low$ and $high$ be $a$ and $b$ respectively.
46
+
47
+
Next, we traverse all lengths in the range $[a,..b]$. For each length $n$, we get all strobogrammatic numbers $dfs(n)$, and then check whether they are in the range $[low, high]$. If they are, we increment the answer.
48
+
49
+
The time complexity is $O(2^{n+2} \times \log n)$.
50
+
51
+
Similar problems:
52
+
53
+
-[247. Strobogrammatic Number II](https://github.com/doocs/leetcode/blob/main/solution/0200-0299/0247.Strobogrammatic%20Number%20II/README_EN.md)
0 commit comments