-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray914.java
More file actions
43 lines (40 loc) · 943 Bytes
/
Array914.java
File metadata and controls
43 lines (40 loc) · 943 Bytes
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
32
33
34
35
36
37
38
39
40
41
42
43
package array;
import java.util.HashMap;
import java.util.Map;
/**
* @ProjectName: leetcode
* @Package: array
* @ClassName: Array914
* @Author: markey
* @Description:
* @Date: 2020/3/27 21:55
* @Version: 1.0
*/
public class Array914 {
public boolean hasGroupsSizeX(int[] deck) {
Map<Integer, Integer> map = new HashMap<>();
for(int i: deck) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
if (map.isEmpty()) {
return false;
}
int X = 0;
for(int key: map.keySet()) {
if (X == 0) {
X = map.get(key);
continue;
}
if (X != map.get(key)) {
X = gcd(X, map.get(key));
}
if (X < 2) {
return false;
}
}
return true;
}
private int gcd(int x, int y) {
return x == 0 ? y : gcd(y % x, x);
}
}