Skip to content

feat: add solutions to lc problem: No.1545 #2071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 111 additions & 1 deletion solution/1500-1599/1545.Find Kth Bit in Nth Binary String/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,132 @@

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

**方法一:分类讨论 + 递归**

我们可以发现,对于 $S_n$,其前半部分和 $S_{n-1}$ 是一样的,而后半部分是 $S_{n-1}$ 的反转取反。因此我们可以设计一个函数 $dfs(n, k)$,表示第 $n$ 个字符串的第 $k$ 位字符。答案即为 $dfs(n, k)$。

函数 $dfs(n, k)$ 的计算过程如下:

- 如果 $k = 1$,那么答案为 $0$;
- 如果 $k$ 是 $2$ 的幂次方,那么答案为 $1$;
- 如果 $k \times 2 \lt 2^n - 1$,说明 $k$ 在前半部分,答案为 $dfs(n - 1, k)$;
- 否则,答案为 $dfs(n - 1, 2^n - k) \oplus 1$,其中 $\oplus$ 表示异或运算。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为题目给定的 $n$。

<!-- tabs:start -->

### **Python3**

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

```python

class Solution:
def findKthBit(self, n: int, k: int) -> str:
def dfs(n: int, k: int) -> int:
if k == 1:
return 0
if (k & (k - 1)) == 0:
return 1
m = 1 << n
if k * 2 < m - 1:
return dfs(n - 1, k)
return dfs(n - 1, m - k) ^ 1

return str(dfs(n, k))
```

### **Java**

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

```java
class Solution {
public char findKthBit(int n, int k) {
return (char) ('0' + dfs(n, k));
}

private int dfs(int n, int k) {
if (k == 1) {
return 0;
}
if ((k & (k - 1)) == 0) {
return 1;
}
int m = 1 << n;
if (k * 2 < m - 1) {
return dfs(n - 1, k);
}
return dfs(n - 1, m - k) ^ 1;
}
}
```

### **C++**

```cpp
class Solution {
public:
char findKthBit(int n, int k) {
function<int(int, int)> dfs = [&](int n, int k) {
if (k == 1) {
return 0;
}
if ((k & (k - 1)) == 0) {
return 1;
}
int m = 1 << n;
if (k * 2 < m - 1) {
return dfs(n - 1, k);
}
return dfs(n - 1, m - k) ^ 1;
};
return '0' + dfs(n, k);
}
};
```

### **Go**

```go
func findKthBit(n int, k int) byte {
var dfs func(n, k int) int
dfs = func(n, k int) int {
if k == 1 {
return 0
}
if k&(k-1) == 0 {
return 1
}
m := 1 << n
if k*2 < m-1 {
return dfs(n-1, k)
}
return dfs(n-1, m-k) ^ 1
}
return byte('0' + dfs(n, k))
}
```

### **TypeScript**

```ts
function findKthBit(n: number, k: number): string {
const dfs = (n: number, k: number): number => {
if (k === 1) {
return 0;
}
if ((k & (k - 1)) === 0) {
return 1;
}
const m = 1 << n;
if (k * 2 < m - 1) {
return dfs(n - 1, k);
}
return dfs(n - 1, m - k) ^ 1;
};
return dfs(n, k).toString();
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,128 @@ The 11<sup>th</sup> bit is &quot;1&quot;.

## Solutions

**Solution 1: Case Analysis + Recursion**

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)$.

The calculation process of the function $dfs(n, k)$ is as follows:

- If $k = 1$, then the answer is $0$;
- If $k$ is a power of $2$, then the answer is $1$;
- If $k \times 2 < 2^n - 1$, it means that $k$ is in the first half, and the answer is $dfs(n - 1, k)$;
- Otherwise, the answer is $dfs(n - 1, 2^n - k) \oplus 1$, where $\oplus$ represents the XOR operation.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the given $n$ in the problem.

<!-- tabs:start -->

### **Python3**

```python

class Solution:
def findKthBit(self, n: int, k: int) -> str:
def dfs(n: int, k: int) -> int:
if k == 1:
return 0
if (k & (k - 1)) == 0:
return 1
m = 1 << n
if k * 2 < m - 1:
return dfs(n - 1, k)
return dfs(n - 1, m - k) ^ 1

return str(dfs(n, k))
```

### **Java**

```java
class Solution {
public char findKthBit(int n, int k) {
return (char) ('0' + dfs(n, k));
}

private int dfs(int n, int k) {
if (k == 1) {
return 0;
}
if ((k & (k - 1)) == 0) {
return 1;
}
int m = 1 << n;
if (k * 2 < m - 1) {
return dfs(n - 1, k);
}
return dfs(n - 1, m - k) ^ 1;
}
}
```

### **C++**

```cpp
class Solution {
public:
char findKthBit(int n, int k) {
function<int(int, int)> dfs = [&](int n, int k) {
if (k == 1) {
return 0;
}
if ((k & (k - 1)) == 0) {
return 1;
}
int m = 1 << n;
if (k * 2 < m - 1) {
return dfs(n - 1, k);
}
return dfs(n - 1, m - k) ^ 1;
};
return '0' + dfs(n, k);
}
};
```

### **Go**

```go
func findKthBit(n int, k int) byte {
var dfs func(n, k int) int
dfs = func(n, k int) int {
if k == 1 {
return 0
}
if k&(k-1) == 0 {
return 1
}
m := 1 << n
if k*2 < m-1 {
return dfs(n-1, k)
}
return dfs(n-1, m-k) ^ 1
}
return byte('0' + dfs(n, k))
}
```

### **TypeScript**

```ts
function findKthBit(n: number, k: number): string {
const dfs = (n: number, k: number): number => {
if (k === 1) {
return 0;
}
if ((k & (k - 1)) === 0) {
return 1;
}
const m = 1 << n;
if (k * 2 < m - 1) {
return dfs(n - 1, k);
}
return dfs(n - 1, m - k) ^ 1;
};
return dfs(n, k).toString();
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
char findKthBit(int n, int k) {
function<int(int, int)> dfs = [&](int n, int k) {
if (k == 1) {
return 0;
}
if ((k & (k - 1)) == 0) {
return 1;
}
int m = 1 << n;
if (k * 2 < m - 1) {
return dfs(n - 1, k);
}
return dfs(n - 1, m - k) ^ 1;
};
return '0' + dfs(n, k);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
func findKthBit(n int, k int) byte {
var dfs func(n, k int) int
dfs = func(n, k int) int {
if k == 1 {
return 0
}
if k&(k-1) == 0 {
return 1
}
m := 1 << n
if k*2 < m-1 {
return dfs(n-1, k)
}
return dfs(n-1, m-k) ^ 1
}
return byte('0' + dfs(n, k))
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,19 @@
class Solution {
public char findKthBit(int n, int k) {
if (k == 1 || n == 1) {
return '0';
}
Set<Integer> set = new HashSet<>();
int len = calcLength(n, set);
if (set.contains(k)) {
return '1';
}
// 中间,返回1
if (k < len / 2) {
return findKthBit(n - 1, k);
} else {
if (set.contains(len - k)) {
return '1';
}
return r(findKthBit(n - 1, len - k + 1));
}
}

private char r(char b) {
if (b == '0') {
return '1';
}
return '0';
}

private int calcLength(int n, Set<Integer> set) {
if (n == 1) {
return 1;
}

int ans = 2 * calcLength(n - 1, set) + 1;
set.add(ans + 1);
return ans;
}
class Solution {
public char findKthBit(int n, int k) {
return (char) ('0' + dfs(n, k));
}

private int dfs(int n, int k) {
if (k == 1) {
return 0;
}
if ((k & (k - 1)) == 0) {
return 1;
}
int m = 1 << n;
if (k * 2 < m - 1) {
return dfs(n - 1, k);
}
return dfs(n - 1, m - k) ^ 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def findKthBit(self, n: int, k: int) -> str:
def dfs(n: int, k: int) -> int:
if k == 1:
return 0
if (k & (k - 1)) == 0:
return 1
m = 1 << n
if k * 2 < m - 1:
return dfs(n - 1, k)
return dfs(n - 1, m - k) ^ 1

return str(dfs(n, k))
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function findKthBit(n: number, k: number): string {
const dfs = (n: number, k: number): number => {
if (k === 1) {
return 0;
}
if ((k & (k - 1)) === 0) {
return 1;
}
const m = 1 << n;
if (k * 2 < m - 1) {
return dfs(n - 1, k);
}
return dfs(n - 1, m - k) ^ 1;
};
return dfs(n, k).toString();
}
Loading