跳过正文
Background Image

力扣 p1128

·49 字·1 分钟
Hypoy
作者
Hypoy
写万行码,行万里路

Problem: 1128. 等价多米诺骨牌对的数量

思路

通过排序将等价多米诺骨牌变为等于多米诺骨牌,然后用哈希记录有多少对即可。

解题过程

先进行sort排序,然后答案累加,计入哈希

复杂度

  • 时间复杂度: O(n)

  • 空间复杂度: O(n)

Code

1
2
3
4
5
6
7
8
9
class Solution:
 def numEquivDominoPairs(self, dominoes: List[List[int]]) -> int:
        cnt = defaultdict(int)
 ans = 0
 for i,x in enumerate(dominoes):
            x.sort()
            ans += cnt[str(x)]
            cnt[str(x)] += 1
 return ans