2275. Largest Combination With Bitwise AND
Problem
The bitwise AND of an array
nums
is the bitwise AND of all integers innums
.
For example, for
nums = [1, 5, 3]
, the bitwise AND is equal to1 & 5 & 3 = 1
.Also, for
nums = [7]
, the bitwise AND is7
.You are given an array of positive integers
candidates
. Evaluate the bitwise AND of every combination of numbers ofcandidates
. Each number incandidates
may only be used once in each combination.Return *the size of the largest combination of
candidates
with a bitwise AND greater than *0
.
Solution
相当于将各个数字进行掩码操作,计算各个数组的同一个位上1的数量即可。
bin[]数组记录各个数字的各个位上的1的数量。
如果当前数字与1进行掩码操作后等于1,则将该位数量加一。
然后将当前数字向右移动一位,直至将所有的位都统计完。
最后返回各个位上的最大值即可。
Code
1 | class Solution { |
2275. Largest Combination With Bitwise AND
https://xuanhe95.github.io/2022/05/15/2275-Largest-Combination-With-Bitwise-AND/