Leetcode-1012 Complement of Base 10 Integer(十进制整数的补码)

 1 #define _for(i,a,b) for(int i = (a);i < (b);i ++)
 2 class Solution
 3 {
 4     public:
 5         int bitwiseComplement(int N)
 6         {
 7             if(N==0)
 8                 return 1;
 9             string s;
10             while(N)
11             {
12                 s+= N%2+'0';
13                 N/=2;
14             }
15             reverse(s.begin(),s.end());
16         
17             _for(i,0,s.size())
18             {
19                 if(s[i]=='0')
20                 {
21                     s[i] = '1';
22                 }
23                 else
24                     s[i] = '0';
25             }
26             
27             int rnt = 0;
28             for(int i = 0 ;i < s.size();i ++)
29             {
30                 rnt += (s[i]-'0')*pow(2,s.size()-i-1);
31               //  cout << rnt << endl;
32             }
33             return rnt;
34         }
35 };

猜你喜欢

转载自www.cnblogs.com/Asurudo/p/10546531.html