Skip to content

Commit 091175e

Browse files
committed
feat: Solve leetcode/leetcoding_challenge/2024/aug2024/week4/number_complement.cpp
1 parent e98bda5 commit 091175e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <algorithm>
2+
#include <vector>
3+
#include <queue>
4+
#include <set>
5+
#include <limits>
6+
#include <map>
7+
#include <unordered_set>
8+
#include <unordered_map>
9+
#include <iterator>
10+
#include <sstream>
11+
#include <iostream> // includes cin to read from stdin and cout to write to stdout
12+
using namespace std; // since cin and cout are both in namespace std, this saves some text
13+
14+
class Solution {
15+
public:
16+
int findComplement(int num) {
17+
int answer = 0;
18+
int base = 1;
19+
while (num > 1) {
20+
if (num % 2 == 0) {
21+
answer += base ;
22+
}
23+
num /= 2;
24+
base *= 2;
25+
}
26+
27+
return answer;
28+
}
29+
};

0 commit comments

Comments
 (0)