110 字
1 分钟

力扣 p1128

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

思路

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

解题过程

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

复杂度

  • 时间复杂度: O(n)

  • 空间复杂度: O(n)

Code

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

文章分享

如果这篇文章对你有帮助,欢迎分享给更多人!

最后更新于 2025-05-04,距今已过 305 天

部分内容可能已过时

Profile Image of the Author
Hypoy
Hello, I'm Hypoy.
公告
欢迎来到我的博客!这是一则示例公告。
分类
标签
站点统计
文章
10
分类
5
标签
23
总字数
10,584
运行时长
0
最后活动
0 天前

目录