这一道题第一眼看上去很懵,仔细一想发现每一位都是独立的,因此枚举每一位判断即可。
class Solution {
public:
int minFlips(int a, int b, int c) {
int res = 0;
for(int i = 0; i < 31; i++){
int bit_a = (a & 1);
int bit_b = (b & 1);
int bit_c = (c & 1);
if((bit_a | bit_b) != bit_c){
if(bit_a == 1 && bit_b == 1 && bit_c == 0){
res+=2;
}else{
res++;
}
}
a = (a >> 1);
b = (b >> 1);
c = (c >> 1);
}
return res;
}
};