Skip to content

Commit 0221437

Browse files
committed
Time: 2 ms (42.58%), Space: 6 MB (30.84%) - LeetHub
1 parent db44f55 commit 0221437

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int bitwiseComplement(int n) {
4+
int m = n;
5+
int mask = 0;
6+
if (n == 0)
7+
return 1;
8+
while(m!=0)
9+
{
10+
mask = (mask << 1) | 1;
11+
m = m >> 1;
12+
}
13+
int ans = (~n) & mask;
14+
return ans;
15+
}
16+
};

0 commit comments

Comments
 (0)