- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1009.java
28 lines (26 loc) · 737 Bytes
/
_1009.java
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
packagecom.fishercoder.solutions.secondthousand;
importjava.util.ArrayList;
importjava.util.List;
publicclass_1009 {
publicstaticclassSolution1 {
publicintbitwiseComplement(intN) {
if (N == 0) {
return1;
}
List<Integer> list = newArrayList<>();
while (N != 0) {
list.add(N & 1);
N >>= 1;
}
intresult = 0;
intexp = list.size() - 1;
for (inti = list.size() - 1; i >= 0; i--) {
if (list.get(i) == 0) {
result += Math.pow(2, exp);
}
exp--;
}
returnresult;
}
}
}