方法三:动态规划——最低有效位
class Solution {
public int[] countBits(int n) {
int[] bits = new int[n+1];
for(int i = 1; i <= n; i++) {
bits[i] = bits[i >> 1] + (i & 1);
}
return bits;
}
}
class Solution {
public int singleNumber(int[] nums) {
int res = 0;
for(int x: nums) {
res = res ^ x;
}
return res;
}
}
class Solution {
public int minFlips(int a, int b, int c) {
int res = 0;
while((long)a + b + c > 0) {
int la = a & 1;
int lb = b & 1;
int lc = c & 1;
a = a >> 1;
b = b >> 1;
c = c >> 1;
if(lc == 0) {
res = res + la + lb;
}else {
if(la + lb >= 1) continue;
else res++;
}
}
return res;
}
}