LeetCode6065.按位与结果大于零的最长组合-----位运算

题目表述

对数组 nums 执行 按位与 相当于对数组 nums 中的所有整数执行 按位与 。

  • 例如,对 nums = [1, 5, 3] 来说,按位与等于 1 & 5 & 3 = 1 。
  • 同样,对 nums = [7] 而言,按位与等于 7 。

给你一个正整数数组 candidates 。计算 candidates 中的数字每种组合下 按位与 的结果。 candidates 中的每个数字在每种组合中只能使用 一次 。

返回按位与结果大于 0 的 最长 组合的长度。

示例1:
输入:candidates = [16,17,71,62,12,24,14]
输出:4
解释:组合 [16,17,62,24] 的按位与结果是 16 & 17 & 62 & 24 = 16 > 0 。
组合长度是 4 。
可以证明不存在按位与结果大于 0 且长度大于 4 的组合。
注意,符合长度最大的组合可能不止一种。
例如,组合 [62,12,24,14] 的按位与结果是 62 & 12 & 24 & 14 = 8 > 0 。

位运算

1 & 1 = 1
1 & 0 = 0
0 & 0 = 0

统计二进制位上1的个数,然后找到指定位1数字最多的即为此题的解。

class Solution {
    public int largestCombination(int[] candidates) {
        int res = 0;
        for(int i = 0; i < 32;i++){
            int bit = 1 << i;
            int num = 0;
            for(int tmp : candidates){
                if((tmp & bit)!=0) num++;
            }
            res = Math.max(res, num);
        }
        return res;
    }
}
posted @ 2022-05-15 22:40  YoungerWb  阅读(103)  评论(0)    收藏  举报