Skip to content

Commit f0e6a77

Browse files
authored
Create 1545-find-kth-bit-in-nth-binary-string.js
1 parent aa7ad7c commit f0e6a77

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number} n
3+
* @param {number} k
4+
* @return {character}
5+
*/
6+
var findKthBit = function(n, k) {
7+
let count = 0, l = (1 << n) - 1;
8+
while (k > 1) {
9+
if (k == Math.floor(l / 2) + 1) return count % 2 == 0 ? '1' : '0';
10+
if (k > l / 2) {
11+
k = l + 1 - k;
12+
count++;
13+
}
14+
l = Math.floor(l / 2);
15+
}
16+
return count % 2 === 0 ? '0' : '1';
17+
};

0 commit comments

Comments
 (0)