一刷 06/2022
Version #1 Bit Operation Iteration
bitwise AND > 0只需要某一位大于0就可以满足,为了使某一位大于0,可以选择所有这一位大于0的数进行AND操作
Time O(N)
Space O(1)
Runtime: 18 ms, faster than 97.25% of Java online submissions for Largest Combination With Bitwise AND Greater Than Zero.
Memory Usage: 84 MB, less than 23.81% of Java online submissions for Largest Combination With Bitwise AND Greater Than Zero.
class Solution {
public int largestCombination(int[] candidates) {
int max = 0;
for (int i = 0; i < 32; i++) {
int cnt = 0;
for (int candidate : candidates) {
cnt += (candidate >> i) & 1;
}
max = Math.max(cnt, max);
}
return max;
}
}
No comments:
Post a Comment