From b51ecbdaf973870f9805f7acd2c38aaa8bba5e9f Mon Sep 17 00:00:00 2001 From: rain84 Date: Sun, 20 Oct 2024 06:04:13 +0300 Subject: [PATCH 1/2] feat: add solutions to lc problem: No.1545 --- .../README.md | 23 +++++++++++++++++++ .../README_EN.md | 23 +++++++++++++++++++ .../Solution2.js | 1 + .../Solution2.ts | 2 ++ 4 files changed, 49 insertions(+) create mode 100644 solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution2.js create mode 100644 solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution2.ts 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..bc9a96a0a296f 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 { + + +### 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/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); From 79bc3166a5145abdc005c97c9026452ea719747a Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sun, 20 Oct 2024 13:17:22 +0800 Subject: [PATCH 2/2] Update README.md --- .../1500-1599/1545.Find Kth Bit in Nth Binary String/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 bc9a96a0a296f..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 @@ -217,7 +217,7 @@ function findKthBit(n: number, k: number): string { -### Solution 2: Bit Manipulation +### 方法二:位运算