Skip to content

Commit 87f3911

Browse files
authored
feat: add solutions to lc problem: No.1545 (#3652)
1 parent 89c1c55 commit 87f3911

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

solution/1500-1599/1545.Find Kth Bit in Nth Binary String/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,27 @@ function findKthBit(n: number, k: number): string {
215215

216216
<!-- solution:end -->
217217

218+
<!-- solution:start -->
219+
220+
### 方法二:位运算
221+
222+
<!-- tabs:start -->
223+
224+
#### TypeScript
225+
226+
```ts
227+
const findKthBit = (n: number, k: number): string =>
228+
String((((k / (k & -k)) >> 1) & 1) ^ (k & 1) ^ 1);
229+
```
230+
231+
#### JavaScript
232+
233+
```js
234+
const findKthBit = (n, k) => String((((k / (k & -k)) >> 1) & 1) ^ (k & 1) ^ 1);
235+
```
236+
237+
<!-- tabs:end -->
238+
239+
<!-- solution:end -->
240+
218241
<!-- problem:end -->

solution/1500-1599/1545.Find Kth Bit in Nth Binary String/README_EN.md

+23
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,27 @@ function findKthBit(n: number, k: number): string {
201201

202202
<!-- solution:end -->
203203

204+
<!-- solution:start -->
205+
206+
### Solution 2: Bit Manipulation
207+
208+
<!-- tabs:start -->
209+
210+
#### TypeScript
211+
212+
```ts
213+
const findKthBit = (n: number, k: number): string =>
214+
String((((k / (k & -k)) >> 1) & 1) ^ (k & 1) ^ 1);
215+
```
216+
217+
#### JavaScript
218+
219+
```js
220+
const findKthBit = (n, k) => String((((k / (k & -k)) >> 1) & 1) ^ (k & 1) ^ 1);
221+
```
222+
223+
<!-- tabs:end -->
224+
225+
<!-- solution:end -->
226+
204227
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const findKthBit = (n, k) => String((((k / (k & -k)) >> 1) & 1) ^ (k & 1) ^ 1);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const findKthBit = (n: number, k: number): string =>
2+
String((((k / (k & -k)) >> 1) & 1) ^ (k & 1) ^ 1);

0 commit comments

Comments
 (0)