-
发表于 2025.01.12
-
注意这里的组合并非是连续元素。可以得知,如果组合中的按位与结果不为0,说明这些元素至少有一个比特位全为1(只有这里才能保证按位与后至少有一个比特位不为0)。因此,我们可以统计每个元素的每个比特位的1的个数,然后取最大值即可。
class Solution { public: int largestCombination(vector<int>& candidates) { int cnt[32] {0}; for (auto cand : candidates) { for (int i = 0; i < 32; ++i) { cnt[i] += (cand & 1); cand >>= 1; } } return *max_element(cnt, cnt + 32); } };
- LC 题目链接
-