1128. Number of Equivalent Domino Pairs

Given a list of dominoesdominoes[i] = [a, b] is equivalent to dominoes[j] = [c, d] if and only if either (a==c and b==d), or (a==d and b==c) - that is, one domino can be rotated to be equal to another domino.

Return the number of pairs (i, j) for which 0 <= i < j < dominoes.length, and dominoes[i] is equivalent to dominoes[j].

Constraints:

  • 1 <= dominoes.length <= 40000
  • 1 <= dominoes[i][j] <= 9

因为数字范围是1-9,所以两位我打算把他们两两排序然后x*10+y拼成两位数,然后用一个计数数组存出现的次数

class Solution(object):
    def numEquivDominoPairs(self, dominoes):
        """
        :type dominoes: List[List[int]]
        :rtype: int
        """
        d = [0] * 100
        ans = 0
        for dominoe in dominoes:
            x = min(dominoe[0], dominoe[1])
            y = max(dominoe[0], dominoe[1])
            ans += d[x * 10 + y]
            d[x * 10 + y] += 1
        return ans
            
        
原文地址:https://www.cnblogs.com/whatyouthink/p/13296630.html