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
Copy file name to clipboardexpand all lines: solution/1500-1599/1545.Find Kth Bit in Nth Binary String/README_EN.md
+111-1
Original file line number
Diff line number
Diff line change
@@ -53,18 +53,128 @@ The 11<sup>th</sup> bit is "1".
53
53
54
54
## Solutions
55
55
56
+
**Solution 1: Case Analysis + Recursion**
57
+
58
+
We can observe that for $S_n$, the first half is the same as $S_{n-1}$, and the second half is the reverse and negation of $S_{n-1}$. Therefore, we can design a function $dfs(n, k)$, which represents the $k$-th character of the $n$-th string. The answer is $dfs(n, k)$.
59
+
60
+
The calculation process of the function $dfs(n, k)$ is as follows:
61
+
62
+
- If $k = 1$, then the answer is $0$;
63
+
- If $k$ is a power of $2$, then the answer is $1$;
64
+
- If $k \times 2 < 2^n - 1$, it means that $k$ is in the first half, and the answer is $dfs(n - 1, k)$;
65
+
- Otherwise, the answer is $dfs(n - 1, 2^n - k) \oplus 1$, where $\oplus$ represents the XOR operation.
66
+
67
+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the given $n$ in the problem.
0 commit comments