8000 Create Question 4 Number Complement · shivamkumar177/LeetcodeMay@ed7165d · GitHub
[go: up one dir, main page]

Skip to content

Commit ed7165d

Browse files
Create Question 4 Number Complement
BIT
1 parent 50ce5fb commit ed7165d

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

WEEK 1/Question 4 Number Complement

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int findComplement(int num) {
4+
string s;
5+
while(num!=0)
6+
{
7+
int r=num%2;
8+
if(r==1)s+='0';
9+
if(r==0)s+='1';
10+
num/=2;
11+
}
12+
reverse(s.begin(),s.end());
13+
int y=1,ans=0;
14+
if(s[s.length()-1]=='1')ans=1;
15+
for(int i=s.length()-2;i>=0;i--)
16+
{
17+
if(s[i]=='1')ans+=pow(2,y);
18+
y++;
19+
}
20+
return ans;
21+
}
22+
};

0 commit comments

Comments
 (0)
0