Skip to content

Commit 22f3385

Browse files
add 1009
1 parent bd99d3b commit 22f3385

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Your ideas/fixes/algorithms are more than welcome!
3131
|1021|[Remove Outermost Parentheses](https://leetcode.com/problems/remove-outermost-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1021.java) | O(n) | O(n) | |Easy|
3232
|1018|[Binary Prefix Divisible By 5](https://leetcode.com/problems/binary-prefix-divisible-by-5/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1018.java) | O(n) | O(1) | |Easy|
3333
|1013|[Pairs of Songs With Total Durations Divisible by 60](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1013.java) | O(n) | O(1) | |Easy|
34+
|1009|[Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1009.java) | O(n) | O(1) | |Easy|
3435
|1002|[Find Common Characters](https://leetcode.com/problems/find-common-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1002.java) | O(n) | O(1) | |Easy|
3536
|999|[Available Captures for Rook](https://leetcode.com/problems/available-captures-for-rook/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_999.java) | O(1) | O(1) | |Easy|
3637
|997|[Find the Town Judge](https://leetcode.com/problems/find-the-town-judge/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_997.java) | O(n) | O(n) | |Easy|
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 1009. Complement of Base 10 Integer
8+
*
9+
* Every non-negative integer N has a binary representation.
10+
* For example, 5 can be represented as "101" in binary,
11+
* 11 as "1011" in binary, and so on.
12+
*
13+
* Note that except for N = 0, there are no leading zeroes in any binary representation.
14+
*
15+
* The complement of a binary representation is the number in binary you get when
16+
* changing every 1 to a 0 and 0 to a 1. For example, the complement of "101" in binary is "010" in binary.
17+
*
18+
* For a given number N in base-10, return the complement of it's binary representation as a base-10 integer.
19+
*
20+
* Example 1:
21+
* Input: 5
22+
* Output: 2
23+
* Explanation: 5 is "101" in binary, with complement "010" in binary, which is 2 in base-10.
24+
*
25+
* Example 2:
26+
* Input: 7
27+
* Output: 0
28+
* Explanation: 7 is "111" in binary, with complement "000" in binary, which is 0 in base-10.
29+
*
30+
* Example 3:
31+
* Input: 10
32+
* Output: 5
33+
* Explanation: 10 is "1010" in binary, with complement "0101" in binary, which is 5 in base-10.
34+
*
35+
* Note:
36+
* 0 <= N < 10^9
37+
* */
38+
public class _1009 {
39+
public static class Solution1 {
40+
public int bitwiseComplement(int N) {
41+
if (N == 0) {
42+
return 1;
43+
}
44+
List<Integer> list = new ArrayList<>();
45+
while (N != 0) {
46+
list.add(N & 1);
47+
N >>= 1;
48+
}
49+
int result = 0;
50+
int exp = list.size() - 1;
51+
for (int i = list.size() - 1; i >= 0; i--) {
52+
if (list.get(i) == 0) {
53+
result += Math.pow(2, exp);
54+
}
55+
exp--;
56+
}
57+
return result;
58+
}
59+
}
60+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1009;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1009Test {
10+
private static _1009.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1009.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(2, solution1.bitwiseComplement(5));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(5, solution1.bitwiseComplement(10));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(0, solution1.bitwiseComplement(7));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(3, solution1.bitwiseComplement(12));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(1, solution1.bitwiseComplement(0));
40+
}
41+
42+
}

0 commit comments

Comments
 (0)