LeetCode——1128. 等价多米诺骨牌对的数量

class Solution:
    def numEquivDominoPairs(self, dominoes: List[List[int]]) -> int:
        if not dominoes:
            return 0
        
        dic = defaultdict(int)
        for x,y in dominoes:
            if x<y:
                dic[(x,y)]+=1
            else:
                dic[(y,x)]+=1
        
        res = 0
        for k,v in dic.items():
            tmp = (v*(v-1))//2
            res+=tmp
        return res
  • 利用hash表存储出现的次数
  • 然后计算有多少对即可 
  • easy题重拳出击

猜你喜欢

转载自blog.csdn.net/weixin_37724529/article/details/113172873