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 2366c4ac33d88..4524c2e58f4de 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 @@ -215,4 +215,27 @@ function findKthBit(n: number, k: number): string { + + +### 方法二:位运算 + + + +#### TypeScript + +```ts +const findKthBit = (n: number, k: number): string => + String((((k / (k & -k)) >> 1) & 1) ^ (k & 1) ^ 1); +``` + +#### JavaScript + +```js +const findKthBit = (n, k) => String((((k / (k & -k)) >> 1) & 1) ^ (k & 1) ^ 1); +``` + + + + + 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 45389ab250a6f..fb82d79ee3240 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 @@ -201,4 +201,27 @@ function findKthBit(n: number, k: number): string { + + +### Solution 2: Bit Manipulation + + + +#### TypeScript + +```ts +const findKthBit = (n: number, k: number): string => + String((((k / (k & -k)) >> 1) & 1) ^ (k & 1) ^ 1); +``` + +#### JavaScript + +```js +const findKthBit = (n, k) => String((((k / (k & -k)) >> 1) & 1) ^ (k & 1) ^ 1); +``` + + + + + diff --git a/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution2.js b/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution2.js new file mode 100644 index 0000000000000..9cb6d6e5f78f7 --- /dev/null +++ b/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution2.js @@ -0,0 +1 @@ +const findKthBit = (n, k) => String((((k / (k & -k)) >> 1) & 1) ^ (k & 1) ^ 1); diff --git a/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution2.ts b/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution2.ts new file mode 100644 index 0000000000000..dc914608bd9e5 --- /dev/null +++ b/solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution2.ts @@ -0,0 +1,2 @@ +const findKthBit = (n: number, k: number): string => + String((((k / (k & -k)) >> 1) & 1) ^ (k & 1) ^ 1);