-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathFindKthBit.java
More file actions
35 lines (31 loc) · 927 Bytes
/
FindKthBit.java
File metadata and controls
35 lines (31 loc) · 927 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// https://leetcode.com/problems/find-kth-bit-in-nth-binary-string/
public class KthBit {
public static void main(String[] args) {
System.out.println(findKthBit(4, 11));
}
public static char findKthBit(int n, int k) {
if(k==1){
return '0';
}
return findNthString("0", n).charAt(k-1);
}
public static String findNthString(String s, int n){
if(n==0){
return s;
}
s = s + "1" + reverseInverse(s);
return findNthString(s, n-1);
}
public static String reverseInverse(String s){
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '0') {
sb.append('1');
}
else{
sb.append('0');
}
}
return sb.reverse().toString();
}
}