Skip to content

Commit f0e7ac8

Browse files
authored
Create solution.cpp
1 parent 588acbf commit f0e7ac8

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition of commonSetBits API.
3+
* int commonSetBits(int num);
4+
*/
5+
6+
class Solution {
7+
public:
8+
int findNumber() {
9+
int result = 0;
10+
11+
// Iterate over each bit in a 30bit int (1 <= n <= 2^30 - 1)
12+
for (int i=0; i<30; i++)
13+
{
14+
int num = (1 << i);
15+
16+
if (commonSetBits(num))
17+
{
18+
// if bit is set in num then it must be set in the number we are finding too
19+
// So set it in the result
20+
result |= num;
21+
}
22+
}
23+
24+
return result;
25+
}
26+
};

0 commit comments

Comments
 (0)