Skip to content

Commit 14db2a5

Browse files
authored
feat: add solutions to lc problem: No.1545 (doocs#2071)
No.1545.Find Kth Bit in Nth Binary String
1 parent 6e71522 commit 14db2a5

File tree

8 files changed

+306
-40
lines changed

8 files changed

+306
-40
lines changed

solution/1500-1599/1545.Find Kth Bit in Nth Binary String/README.md

+111-1
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,132 @@
7171

7272
<!-- 这里可写通用的实现逻辑 -->
7373

74+
**方法一:分类讨论 + 递归**
75+
76+
我们可以发现,对于 $S_n$,其前半部分和 $S_{n-1}$ 是一样的,而后半部分是 $S_{n-1}$ 的反转取反。因此我们可以设计一个函数 $dfs(n, k)$,表示第 $n$ 个字符串的第 $k$ 位字符。答案即为 $dfs(n, k)$。
77+
78+
函数 $dfs(n, k)$ 的计算过程如下:
79+
80+
- 如果 $k = 1$,那么答案为 $0$;
81+
- 如果 $k$ 是 $2$ 的幂次方,那么答案为 $1$;
82+
- 如果 $k \times 2 \lt 2^n - 1$,说明 $k$ 在前半部分,答案为 $dfs(n - 1, k)$;
83+
- 否则,答案为 $dfs(n - 1, 2^n - k) \oplus 1$,其中 $\oplus$ 表示异或运算。
84+
85+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为题目给定的 $n$。
86+
7487
<!-- tabs:start -->
7588

7689
### **Python3**
7790

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

8093
```python
81-
94+
class Solution:
95+
def findKthBit(self, n: int, k: int) -> str:
96+
def dfs(n: int, k: int) -> int:
97+
if k == 1:
98+
return 0
99+
if (k & (k - 1)) == 0:
100+
return 1
101+
m = 1 << n
102+
if k * 2 < m - 1:
103+
return dfs(n - 1, k)
104+
return dfs(n - 1, m - k) ^ 1
105+
106+
return str(dfs(n, k))
82107
```
83108

84109
### **Java**
85110

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

88113
```java
114+
class Solution {
115+
public char findKthBit(int n, int k) {
116+
return (char) ('0' + dfs(n, k));
117+
}
118+
119+
private int dfs(int n, int k) {
120+
if (k == 1) {
121+
return 0;
122+
}
123+
if ((k & (k - 1)) == 0) {
124+
return 1;
125+
}
126+
int m = 1 << n;
127+
if (k * 2 < m - 1) {
128+
return dfs(n - 1, k);
129+
}
130+
return dfs(n - 1, m - k) ^ 1;
131+
}
132+
}
133+
```
134+
135+
### **C++**
136+
137+
```cpp
138+
class Solution {
139+
public:
140+
char findKthBit(int n, int k) {
141+
function<int(int, int)> dfs = [&](int n, int k) {
142+
if (k == 1) {
143+
return 0;
144+
}
145+
if ((k & (k - 1)) == 0) {
146+
return 1;
147+
}
148+
int m = 1 << n;
149+
if (k * 2 < m - 1) {
150+
return dfs(n - 1, k);
151+
}
152+
return dfs(n - 1, m - k) ^ 1;
153+
};
154+
return '0' + dfs(n, k);
155+
}
156+
};
157+
```
158+
159+
### **Go**
160+
161+
```go
162+
func findKthBit(n int, k int) byte {
163+
var dfs func(n, k int) int
164+
dfs = func(n, k int) int {
165+
if k == 1 {
166+
return 0
167+
}
168+
if k&(k-1) == 0 {
169+
return 1
170+
}
171+
m := 1 << n
172+
if k*2 < m-1 {
173+
return dfs(n-1, k)
174+
}
175+
return dfs(n-1, m-k) ^ 1
176+
}
177+
return byte('0' + dfs(n, k))
178+
}
179+
```
89180

181+
### **TypeScript**
182+
183+
```ts
184+
function findKthBit(n: number, k: number): string {
185+
const dfs = (n: number, k: number): number => {
186+
if (k === 1) {
187+
return 0;
188+
}
189+
if ((k & (k - 1)) === 0) {
190+
return 1;
191+
}
192+
const m = 1 << n;
193+
if (k * 2 < m - 1) {
194+
return dfs(n - 1, k);
195+
}
196+
return dfs(n - 1, m - k) ^ 1;
197+
};
198+
return dfs(n, k).toString();
199+
}
90200
```
91201

92202
### **...**

solution/1500-1599/1545.Find Kth Bit in Nth Binary String/README_EN.md

+111-1
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,128 @@ The 11<sup>th</sup> bit is &quot;1&quot;.
5353

5454
## Solutions
5555

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.
68+
5669
<!-- tabs:start -->
5770

5871
### **Python3**
5972

6073
```python
61-
74+
class Solution:
75+
def findKthBit(self, n: int, k: int) -> str:
76+
def dfs(n: int, k: int) -> int:
77+
if k == 1:
78+
return 0
79+
if (k & (k - 1)) == 0:
80+
return 1
81+
m = 1 << n
82+
if k * 2 < m - 1:
83+
return dfs(n - 1, k)
84+
return dfs(n - 1, m - k) ^ 1
85+
86+
return str(dfs(n, k))
6287
```
6388

6489
### **Java**
6590

6691
```java
92+
class Solution {
93+
public char findKthBit(int n, int k) {
94+
return (char) ('0' + dfs(n, k));
95+
}
96+
97+
private int dfs(int n, int k) {
98+
if (k == 1) {
99+
return 0;
100+
}
101+
if ((k & (k - 1)) == 0) {
102+
return 1;
103+
}
104+
int m = 1 << n;
105+
if (k * 2 < m - 1) {
106+
return dfs(n - 1, k);
107+
}
108+
return dfs(n - 1, m - k) ^ 1;
109+
}
110+
}
111+
```
112+
113+
### **C++**
114+
115+
```cpp
116+
class Solution {
117+
public:
118+
char findKthBit(int n, int k) {
119+
function<int(int, int)> dfs = [&](int n, int k) {
120+
if (k == 1) {
121+
return 0;
122+
}
123+
if ((k & (k - 1)) == 0) {
124+
return 1;
125+
}
126+
int m = 1 << n;
127+
if (k * 2 < m - 1) {
128+
return dfs(n - 1, k);
129+
}
130+
return dfs(n - 1, m - k) ^ 1;
131+
};
132+
return '0' + dfs(n, k);
133+
}
134+
};
135+
```
136+
137+
### **Go**
138+
139+
```go
140+
func findKthBit(n int, k int) byte {
141+
var dfs func(n, k int) int
142+
dfs = func(n, k int) int {
143+
if k == 1 {
144+
return 0
145+
}
146+
if k&(k-1) == 0 {
147+
return 1
148+
}
149+
m := 1 << n
150+
if k*2 < m-1 {
151+
return dfs(n-1, k)
152+
}
153+
return dfs(n-1, m-k) ^ 1
154+
}
155+
return byte('0' + dfs(n, k))
156+
}
157+
```
67158

159+
### **TypeScript**
160+
161+
```ts
162+
function findKthBit(n: number, k: number): string {
163+
const dfs = (n: number, k: number): number => {
164+
if (k === 1) {
165+
return 0;
166+
}
167+
if ((k & (k - 1)) === 0) {
168+
return 1;
169+
}
170+
const m = 1 << n;
171+
if (k * 2 < m - 1) {
172+
return dfs(n - 1, k);
173+
}
174+
return dfs(n - 1, m - k) ^ 1;
175+
};
176+
return dfs(n, k).toString();
177+
}
68178
```
69179

70180
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
char findKthBit(int n, int k) {
4+
function<int(int, int)> dfs = [&](int n, int k) {
5+
if (k == 1) {
6+
return 0;
7+
}
8+
if ((k & (k - 1)) == 0) {
9+
return 1;
10+
}
11+
int m = 1 << n;
12+
if (k * 2 < m - 1) {
13+
return dfs(n - 1, k);
14+
}
15+
return dfs(n - 1, m - k) ^ 1;
16+
};
17+
return '0' + dfs(n, k);
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func findKthBit(n int, k int) byte {
2+
var dfs func(n, k int) int
3+
dfs = func(n, k int) int {
4+
if k == 1 {
5+
return 0
6+
}
7+
if k&(k-1) == 0 {
8+
return 1
9+
}
10+
m := 1 << n
11+
if k*2 < m-1 {
12+
return dfs(n-1, k)
13+
}
14+
return dfs(n-1, m-k) ^ 1
15+
}
16+
return byte('0' + dfs(n, k))
17+
}
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,19 @@
1-
class Solution {
2-
public char findKthBit(int n, int k) {
3-
if (k == 1 || n == 1) {
4-
return '0';
5-
}
6-
Set<Integer> set = new HashSet<>();
7-
int len = calcLength(n, set);
8-
if (set.contains(k)) {
9-
return '1';
10-
}
11-
// 中间,返回1
12-
if (k < len / 2) {
13-
return findKthBit(n - 1, k);
14-
} else {
15-
if (set.contains(len - k)) {
16-
return '1';
17-
}
18-
return r(findKthBit(n - 1, len - k + 1));
19-
}
20-
}
21-
22-
private char r(char b) {
23-
if (b == '0') {
24-
return '1';
25-
}
26-
return '0';
27-
}
28-
29-
private int calcLength(int n, Set<Integer> set) {
30-
if (n == 1) {
31-
return 1;
32-
}
33-
34-
int ans = 2 * calcLength(n - 1, set) + 1;
35-
set.add(ans + 1);
36-
return ans;
37-
}
1+
class Solution {
2+
public char findKthBit(int n, int k) {
3+
return (char) ('0' + dfs(n, k));
4+
}
5+
6+
private int dfs(int n, int k) {
7+
if (k == 1) {
8+
return 0;
9+
}
10+
if ((k & (k - 1)) == 0) {
11+
return 1;
12+
}
13+
int m = 1 << n;
14+
if (k * 2 < m - 1) {
15+
return dfs(n - 1, k);
16+
}
17+
return dfs(n - 1, m - k) ^ 1;
18+
}
3819
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def findKthBit(self, n: int, k: int) -> str:
3+
def dfs(n: int, k: int) -> int:
4+
if k == 1:
5+
return 0
6+
if (k & (k - 1)) == 0:
7+
return 1
8+
m = 1 << n
9+
if k * 2 < m - 1:
10+
return dfs(n - 1, k)
11+
return dfs(n - 1, m - k) ^ 1
12+
13+
return str(dfs(n, k))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function findKthBit(n: number, k: number): string {
2+
const dfs = (n: number, k: number): number => {
3+
if (k === 1) {
4+
return 0;
5+
}
6+
if ((k & (k - 1)) === 0) {
7+
return 1;
8+
}
9+
const m = 1 << n;
10+
if (k * 2 < m - 1) {
11+
return dfs(n - 1, k);
12+
}
13+
return dfs(n - 1, m - k) ^ 1;
14+
};
15+
return dfs(n, k).toString();
16+
}

0 commit comments

Comments
 (0)