-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path476.java
More file actions
40 lines (37 loc) · 1.21 KB
/
476.java
File metadata and controls
40 lines (37 loc) · 1.21 KB
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
36
37
38
39
40
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
public int findComplement(int num) {
num^=((Integer.highestOneBit(num) << 1) - 1);
return num;
}
}
__________________________________________________________________________________________________
sample 1 ms submission
class Solution {
public int findComplement(int num) {
String num1=Integer.toString(num,2);
StringBuilder sb=new StringBuilder();
for(int i=0;i<num1.length();i++)
{
sb.append(num1.charAt(i)=='0'?1:0);
}
return Integer.parseInt(sb.toString(),2);
}
}
__________________________________________________________________________________________________
sample 31788 kb submission
class Solution {
public int findComplement(int num) {
StringBuilder result = new StringBuilder();
while (num > 0) {
if (num % 2 == 1) {
result.append('0');
} else {
result.append('1');
}
num = num >>> 1;
}
return Integer.parseInt(result.reverse().toString(), 2);
}
}