Skip to content

Commit c280e85

Browse files
committed
1009. Complement of Base 10 Integer
1 parent 38d2995 commit c280e85

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

complement-of-base-10-integer.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Runtime: 0 ms
2+
// Memory Usage: 8.3 MB
3+
class Solution {
4+
public:
5+
int bitwiseComplement(int N) {
6+
if (!N) return 1;
7+
vector<bool> binary;
8+
while (N) {
9+
binary.push_back((N % 2 + 1) % 2);
10+
N /= 2;
11+
}
12+
int res = 0;
13+
for (int i = 0; i < binary.size(); i++) {
14+
res += binary[i] * pow(2, i);
15+
}
16+
return res;
17+
}
18+
};

0 commit comments

Comments
 (0)