跳到主要内容

2275.按位与结果大于零的最长组合

链接:2275.按位与结果大于零的最长组合
难度:Medium
标签:位运算、数组、哈希表、计数
简介:返回按位与结果大于 0 的 最长 组合的长度。

题解 1 - python

  • 编辑时间:2025-01-12
  • 执行用时:603ms
  • 内存消耗:27.18MB
  • 编程语言:python
  • 解法介绍:遍历每一位,判断当前位存在1的数字数量,最大值即答案
class Solution:
def largestCombination(self, candidates: List[int]) -> int:
res = 0
for i in range(32):
cnt = 0
for v in candidates:
if v >> i & 1:
cnt += 1
res = max(res, cnt)
return res