Skip to content

Commit df995aa

Browse files
add 1545
1 parent e22d545 commit df995aa

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1545|[Find Kth Bit in Nth Binary String](https://leetcode.com/problems/find-kth-bit-in-nth-binary-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1545.java) | |Medium|String|
1112
|1544|[Make The String Great](https://leetcode.com/problems/make-the-string-great/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1544.java) | |Easy|String, Stack|
1213
|1541|[Minimum Insertions to Balance a Parentheses String](https://leetcode.com/problems/minimum-insertions-to-balance-a-parentheses-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1541.java) | |Medium|String, Stack|
1314
|1539|[Kth Missing Positive Number](https://leetcode.com/problems/kth-missing-positive-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1539.java) | |Easy|Array, HashTable|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1545 {
4+
public static class Solution1 {
5+
public char findKthBit(int n, int k) {
6+
String[] strings = new String[n];
7+
strings[0] = "0";
8+
for (int i = 1; i < n; i++) {
9+
strings[i] = formString(strings[i - 1]);
10+
}
11+
return strings[n - 1].charAt(k - 1);
12+
}
13+
14+
private String formString(String str) {
15+
return str + "1" + reverse(invert(str));
16+
}
17+
18+
private String reverse(String str) {
19+
StringBuilder sb = new StringBuilder();
20+
return sb.append(str).reverse().toString();
21+
}
22+
23+
private String invert(String str) {
24+
StringBuilder sb = new StringBuilder();
25+
for (char c : str.toCharArray()) {
26+
if (c == '1') {
27+
sb.append('0');
28+
} else {
29+
sb.append('1');
30+
}
31+
}
32+
return sb.toString();
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1545;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1545Test {
10+
private static _1545.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1545.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals('0', solution1.findKthBit(3, 1));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals('1', solution1.findKthBit(4, 11));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals('0', solution1.findKthBit(1, 1));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals('1', solution1.findKthBit(2, 3));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)