-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_3199.java
31 lines (29 loc) · 1.02 KB
/
_3199.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.fishercoder.solutions.fourththousand;
public class _3199 {
public static class Solution1 {
public int tripletCount(int[] a, int[] b, int[] c) {
int count = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < b.length; j++) {
for (int k = 0; k < c.length; k++) {
int xor = a[i] ^ b[j] ^ c[k];
if (evenSetBits(xor)) {
count++;
}
}
}
}
return count;
}
private boolean evenSetBits(int num) {
int bits = 0;
// this is the idea of calculating hamming weight:
// https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_191.java#L16_L23
while (num != 0) {
bits++;
num &= num - 1;
}
return bits % 2 == 0;
}
}
}