LeetCode_1021_ Complement of Base 10 Integer

求十进制数的反码

通过观察可知,输入与输出的关系为:

Input+Output=LeetCode_1021_ Complement of Base 10 Integer

其中n为使LeetCode_1021_ Complement of Base 10 IntegerLeetCode_1021_ Complement of Base 10 IntegerInput+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时的特殊情况讨论即可。

LeetCode_1021_ Complement of Base 10 Integer