forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_476.java
More file actions
26 lines (23 loc) · 749 Bytes
/
_476.java
File metadata and controls
26 lines (23 loc) · 749 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
package com.fishercoder.solutions;
public class _476 {
public static class Solution1 {
public int findComplement(int num) {
return ~num & ((Integer.highestOneBit(num) << 1) - 1);
}
}
public static class Solution2 {
public int findComplement(int num) {
String str = Integer.toBinaryString(num);
StringBuilder sb = new StringBuilder();
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (chars[i] == '0') {
sb.append("1");
} else {
sb.append("0");
}
}
return Integer.parseInt(sb.toString(), 2);
}
}
}