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

阅读 59

2022-02-12

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)

0 0 举报