首页 > 其他 > 详细

leetcode146周赛-5130-等价多米诺骨牌对的数量

时间:2019-07-21 15:54:44      阅读:89      评论:0      收藏:0      [点我收藏+]

题目描述:

技术分享图片

方法一:

class Solution(object):
    def numEquivDominoPairs(self, dominoes):
        """
        :type dominoes: List[List[int]]
        :rtype: int
        """
        f = {}
        ret = 0
        for d in dominoes:
            if d[0] > d[1]:
                d[0], d[1] = d[1], d[0]
            x = d[0] * 10 + d[1]
            ret += f.get(x, 0)
            f[x] = f.get(x, 0) + 1
        return ret

另:

class Solution:
    def numEquivDominoPairs(self, dominoes: List[List[int]]) -> int:
        hashmap = collections.defaultdict(int)
        for i, j in dominoes:
            if i > j:
                i, j = j, i
            hashmap[(i,j)] += 1
        ans = 0
        for key in hashmap:
            ans += hashmap[key]*(hashmap[key]-1)//2
        return an

 

leetcode146周赛-5130-等价多米诺骨牌对的数量

原文:https://www.cnblogs.com/oldby/p/11221314.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!