Skip to content

Commit 48e41fb

Browse files
committed
recursion
1 parent bec3f77 commit 48e41fb

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

‎Med1545.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Med1545 {
2+
public char findKthBit(int n, int k) {
3+
if (n == 1)
4+
return '0';
5+
final int midIndex = (int) Math.pow(2, n - 1); // 1-indexed
6+
if (k == midIndex)
7+
return '1';
8+
if (k < midIndex)
9+
return findKthBit(n - 1, k);
10+
return findKthBit(n - 1, midIndex * 2 - k) == '0' ? '1' : '0';
11+
}
12+
}
13+
14+

0 commit comments

Comments
 (0)