-
Notifications
You must be signed in to change notification settings - Fork 0
/
1128. Number of Equivalent Domino Pairs
92 lines (61 loc) · 1.67 KB
/
1128. Number of Equivalent Domino Pairs
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class Solution {
public int numEquivDominoPairs(int[][] dominoes)
{
int result = 0;
int[][] pairs = new int[9][9];
for(int[] dom: dominoes)
{
pairs[dom[0] - 1][dom[1] - 1]++;
}
for(int i = 0; i < 9; i++)
{
for(int j = i; j < 9; j++)
{
int count = pairs[i][j];
if(i != j)
{
count += pairs[j][i];
}
result += (count * (count - 1)) / 2;
}
}
return result;
/*
int result = 0;
HashMap<Integer, Integer> count = new HashMap<>();
for(int[] dom : dominoes)
{
int key = Math.min(dom[0], dom[1]) * 10 + Math.max(dom[0], dom[1]);
result += count.getOrDefault(key, 0);
count.merge(key, 1, Integer::sum);
}
return result;
*/
/*
int n = dominoes.length;
int i=0, count=0;
HashMap<String, Integer> map = new HashMap<>();
while(i < n)
{
String temp = "";
if(dominoes[i][0] > dominoes[i][1])
{
temp += dominoes[i][1];
temp += dominoes[i][0];
}
else
{
temp += dominoes[i][0];
temp += dominoes[i][1];
}
if(map.containsKey(temp))
{
count += map.get(temp);
}
map.put(temp, map.getOrDefault(temp, 0) + 1);
i++;
}
return count;
*/
}
}