LeetCode_1021_ Complement of Base 10 Integer
求十进制数的反码
通过观察可知,输入与输出的关系为:
Input+Output=
其中n为使Input+Output成立的最小m值
class Solution {
public:
int bitwiseComplement(int N) {
if(N<=1) return 1-N;
int cnt=1;
while(cnt<=N)
cnt=cnt*2;
return cnt-1-N;
}
};
注意对N=0,1时的特殊情况讨论即可。