diff --git a/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/README.md b/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/README.md index 3937afbbbdfae..0b5ffeb52f948 100644 --- a/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/README.md +++ b/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/README.md @@ -71,6 +71,19 @@ +**方法一:分类讨论 + 递归** + +我们可以发现,对于 $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$。 + ### **Python3** @@ -78,7 +91,19 @@ ```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** @@ -86,7 +111,92 @@ ```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 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(); +} ``` ### **...** diff --git a/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/README_EN.md b/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/README_EN.md index b8ac436c71f6a..6456248f0b814 100644 --- a/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/README_EN.md +++ b/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/README_EN.md @@ -53,18 +53,128 @@ The 11th bit is "1". ## 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. + ### **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 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(); +} ``` ### **...** diff --git a/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution.cpp b/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution.cpp new file mode 100644 index 0000000000000..c9cc15ae97fd2 --- /dev/null +++ b/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + char findKthBit(int n, int k) { + function 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); + } +}; \ No newline at end of file diff --git a/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution.go b/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution.go new file mode 100644 index 0000000000000..812a0dd923e2a --- /dev/null +++ b/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution.go @@ -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)) +} \ No newline at end of file diff --git a/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution.java b/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution.java index 313750f245083..3b84fa85d9816 100644 --- a/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution.java +++ b/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution.java @@ -1,38 +1,19 @@ -class Solution { - public char findKthBit(int n, int k) { - if (k == 1 || n == 1) { - return '0'; - } - Set 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 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; + } } \ No newline at end of file diff --git a/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution.py b/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution.py new file mode 100644 index 0000000000000..39255d33cc1a4 --- /dev/null +++ b/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution.py @@ -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)) diff --git a/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution.ts b/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution.ts new file mode 100644 index 0000000000000..c2394238f790f --- /dev/null +++ b/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution.ts @@ -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(); +} diff --git a/solution/2000-2099/2008.Maximum Earnings From Taxi/README.md b/solution/2000-2099/2008.Maximum Earnings From Taxi/README.md index bd1aaacbb6b79..741ce76fba85d 100644 --- a/solution/2000-2099/2008.Maximum Earnings From Taxi/README.md +++ b/solution/2000-2099/2008.Maximum Earnings From Taxi/README.md @@ -53,7 +53,7 @@ **方法一:记忆化搜索 + 二分查找** -我们先将$rides$ 按照$start$ 从小到大排序,然后设计一个函数 $dfs(i)$,表示从第 $i$ 个乘客开始接单,最多能获得的小费。答案即为 $dfs(0)$。 +我们先将 $rides$ 按照$start$ 从小到大排序,然后设计一个函数 $dfs(i)$,表示从第 $i$ 个乘客开始接单,最多能获得的小费。答案即为 $dfs(0)$。 函数 $dfs(i)$ 的计算过程如下: