样例题目:按位与结果大于零的最长组合
//计算从低到高第K个二进制位数值位1的元素个数
public int maxlen(int[] candidates, int k) {
int res = 0;
for (int num : candidates) {
//(1 << k)只有指定来判断num二进制位,很巧妙。
if ((num & (1 << k)) != 0) {
res++;
}
}
return res;
}
public int largestCombination(int[] candidates) {
int res = 0;
for (int i = 0; i < 24; i++) {
//遍历二进制位
res = Math.max(res, maxlen(candidates, i));
}
return res;
}
}
标签:二进制位,num,二进制,指定,int,判断,candidates
From: https://www.cnblogs.com/arioya/p/18667564