0
点赞
收藏
分享

微信扫一扫

【leetcode每日一题】2022.02.09第2006题 差的绝对值为 K 的数对数目--hashmap

是归人不是过客 2022-02-12 阅读 59
class Solution(object):
    def countKDifference(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        暴力?dict吧
        """
        ans=0
        dict_temp={}
        for i in nums:
            if dict_temp.has_key(i):
                dict_temp[i]+=1
            else:
                dict_temp[i]=1
        for i in dict_temp:
            if dict_temp.has_key(i+k):
                ans+=dict_temp[i]*dict_temp[i+k]
        return ans
举报

相关推荐

0 条评论