var numEquivDominoPairs = function(dominoes) { const num = newArray(100).fill(0); let ret = 0; for (const domino of dominoes) { const val = domino[0] < domino[1] ? domino[0] * 10 + domino[1] : domino[1] * 10 + domino[0]; ret += num[val]; num[val]++; } return ret; };
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12
funcnumEquivDominoPairs(dominoes [][]int) (ans int) { cnt := [100]int{} for _, d := range dominoes { if d[0] > d[1] { d[0], d[1] = d[1], d[0] } v := d[0]*10 + d[1] ans += cnt[v] cnt[v]++ } return }
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11
intnumEquivDominoPairs(int** dominoes, int dominoesSize, int* dominoesColSize) { int num[100]; memset(num, 0, sizeof(num)); int ret = 0; for (int i = 0; i < dominoesSize; i++) { int val = dominoes[i][0] < dominoes[i][1] ? dominoes[i][0] * 10 + dominoes[i][1] : dominoes[i][1] * 10 + dominoes[i][0]; ret += num[val]; num[val]++; } return ret; }
[sol1-Python3]
1 2 3 4 5 6 7 8 9
classSolution: defnumEquivDominoPairs(self, dominoes: List[List[int]]) -> int: num = [0] * 100 ret = 0 for x, y in dominoes: val = (x * 10 + y if x <= y else y * 10 + x) ret += num[val] num[val] += 1 return ret