⬆︎
×

[LC] 2275 Largest Combination With Bitwise AND Greater Than Zero

Hyplus目录

Java

import java.util.Arrays;

/**
 * <a href="https://leetcode.cn/problems/largest-combination-with-bitwise-and-greater-than-zero/">Largest Combination With Bitwise AND Greater Than Zero</a>
 * 位运算;数组;哈希表;计数
 */
class Solution {
    public int largestCombination(int[] candidates) {
        int[] cnt = new int[24];
        for (int c : candidates) {
            for (int i = 0; c > 0; i++) {
                cnt[i] += c & 1;
                c >>= 1;
            }
        }
        return Arrays.stream(cnt).max().getAsInt();
    }
}

Go

import "slices"

func largestCombination(candidates []int) int {
    cnt := [24]int{0}
    for _, v := range candidates {
        for i := 0; v > 0; i++ {
            cnt[i] += v & 1
            v >>= 1
        }
    }
    return slices.Max(cnt[:])
}

发表评论