Skip to content

Commit a596f65

Browse files
committed
Time: 3 ms (41.07%), Space: 7.6 MB (33.08%) - LeetHub
1 parent 87114d0 commit a596f65

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int findComplement(int num) {
4+
5+
int ans = 0;
6+
bool ok = false;
7+
8+
for(int i = 31; i >= 0; --i)
9+
{
10+
if(!(num & (1 << i)) and ok)
11+
{
12+
ans += (1 << i);
13+
}
14+
15+
if(num & (1 << i))
16+
{
17+
ok = true;
18+
}
19+
}
20+
21+
return ans;
22+
}
23+
};

0 commit comments

Comments
 (0)