Skip to main content
  1. Posts/

LeetCode-1726 同积元组

·1 min·

LeetCode-1726 同积元组 #

Solution 1 #

用哈希统计.

class Solution:
    def tupleSameProduct(self, nums: List[int]) -> int:
        cnt = {}
        for i in range(len(nums)):
            for j in range(i + 1, len(nums)):
                product = nums[i] * nums[j]
                if product in cnt:
                    cnt[product] += 1
                else:
                    cnt[product] = 1
        ans = 0
        for v in cnt.values():
            ans += v * (v - 1) * 4
        return ans