|
| 1 | +# [1545. 找出第 N 个二进制字符串中的第 K 位](https://leetcode.com/problems/find-kth-bit-in-nth-binary-string) |
| 2 | + |
| 3 | +🟠 <font color=#ffb800>Medium</font>  🔖  [`递归`](/leetcode/outline/tag/recursion.md) [`字符串`](/leetcode/outline/tag/string.md) [`模拟`](/leetcode/outline/tag/simulation.md)  🔗 [`LeetCode`](https://leetcode.com/problems/find-kth-bit-in-nth-binary-string) |
| 4 | + |
| 5 | +## 题目 |
| 6 | + |
| 7 | +Given two positive integers `n` and `k`, the binary string `Sn` is formed as |
| 8 | +follows: |
| 9 | + |
| 10 | +- `S1 = "0"` |
| 11 | +- `Si = Si - 1 + "1" + reverse(invert(Si - 1))` for `i > 1` |
| 12 | + |
| 13 | +Where `+` denotes the concatenation operation, `reverse(x)` returns the |
| 14 | +reversed string `x`, and `invert(x)` inverts all the bits in `x` (`0` changes |
| 15 | +to `1` and `1` changes to `0`). |
| 16 | + |
| 17 | +For example, the first four strings in the above sequence are: |
| 18 | + |
| 19 | +- `S1 = "0"` |
| 20 | +- `S2 = "011"` |
| 21 | +- `S3 = "0111001"` |
| 22 | +- `S4 = "011100110110001"` |
| 23 | + |
| 24 | +Return _the_ `kth` _bit_ _in_ `Sn`. It is guaranteed that `k` is valid for the |
| 25 | +given `n`. |
| 26 | + |
| 27 | +**Example 1:** |
| 28 | + |
| 29 | +> Input: n = 3, k = 1 |
| 30 | +> |
| 31 | +> Output: "0" |
| 32 | +> |
| 33 | +> Explanation: S3 is "**_0_** 111001". |
| 34 | +> |
| 35 | +> The 1st bit is "0". |
| 36 | +
|
| 37 | +**Example 2:** |
| 38 | + |
| 39 | +> Input: n = 4, k = 11 |
| 40 | +> |
| 41 | +> Output: "1" |
| 42 | +> |
| 43 | +> Explanation: S4 is "0111001101** _1_** 0001". |
| 44 | +> |
| 45 | +> The 11th bit is "1". |
| 46 | +
|
| 47 | +**Constraints:** |
| 48 | + |
| 49 | +- `1 <= n <= 20` |
| 50 | +- `1 <= k <= 2^n - 1` |
| 51 | + |
| 52 | +## 题目大意 |
| 53 | + |
| 54 | +给你两个正整数 `n` 和 `k`,二进制字符串 `Sn` 的形成规则如下: |
| 55 | + |
| 56 | +- `S1 = "0"` |
| 57 | +- 当 `i > 1` 时,`Si = Si-1 + "1" + reverse(invert(Si-1))` |
| 58 | + |
| 59 | +其中 `+` 表示串联操作,`reverse(x)` 返回反转 `x` 后得到的字符串,而 `invert(x)` 则会翻转 x 中的每一位(0 变为 |
| 60 | +1,而 1 变为 0)。 |
| 61 | + |
| 62 | +例如,符合上述描述的序列的前 4 个字符串依次是: |
| 63 | + |
| 64 | +- `S1 = "0"` |
| 65 | +- `S2 = "011"` |
| 66 | +- `S3 = "0111001"` |
| 67 | +- `S4 = "011100110110001"` |
| 68 | + |
| 69 | +请你返回 `Sn` 的 **第`k` 位字符** ,题目数据保证 `k` 一定在 `Sn` 长度范围以内。 |
| 70 | + |
| 71 | +**提示:** |
| 72 | + |
| 73 | +- `1 <= n <= 20` |
| 74 | +- `1 <= k <= 2^n - 1` |
| 75 | + |
| 76 | +## 解题思路 |
| 77 | + |
| 78 | +- 可以使用递归方法生成第 `n` 个二进制字符串。生成的过程可以用 `S(n) = S(n-1) + '1' + reverse(invert(Si - 1))` 来表示,其中 `reverse` 是翻转操作,`invert` 是取反操作,通过 `split` 和 `map` 方法实现取反操作。 |
| 79 | + |
| 80 | +- 一旦生成了第 `n` 个字符串,就可以根据 1-indexed 规则返回第 `k` 位的字符。 |
| 81 | + |
| 82 | +#### 复杂度分析 |
| 83 | + |
| 84 | +- **时间复杂度**:`O(2^n)`,随着 `n` 增加,字符串长度呈指数增长。 |
| 85 | +- **空间复杂度**:`O(2^n)`,存储生成的字符串所需的空间。 |
| 86 | + |
| 87 | +## 代码 |
| 88 | + |
| 89 | +```javascript |
| 90 | +/** |
| 91 | + * @param {number} n |
| 92 | + * @param {number} k |
| 93 | + * @return {character} |
| 94 | + */ |
| 95 | +var findKthBit = function (n, k) { |
| 96 | + const genString = (n) => { |
| 97 | + if (n == 1) return '0'; |
| 98 | + const prev = genString(n - 1); |
| 99 | + const reverse = prev |
| 100 | + .split('') |
| 101 | + .map((i) => (i == '0' ? '1' : '0')) |
| 102 | + .reverse() |
| 103 | + .join(''); |
| 104 | + return prev + '1' + reverse; |
| 105 | + }; |
| 106 | + |
| 107 | + const str = genString(n); |
| 108 | + |
| 109 | + return str[k - 1]; // k 是 1-indexed |
| 110 | +}; |
| 111 | +``` |
0 commit comments