思路
- 逐个位进行运算,如果c当前为1,只要a和b当前位任意一个为1即可,当c当前位为0,则只需a和b当前位任意一位为1即可,但是不可以同时为1.
代码
或运算的最小翻转次数public int minFlips(int a, int b, int c) {int res = 0;while (a != 0 || b != 0 || c != 0) {int t = (a & 1) + (b & 1);res += (c & 1) == 0 ? t : t == 0 ? 1 : 0;a >>= 1;b >>= 1;c >>= 1;}return res;}
