题目
Given an array of integers nums.
A pair (i,j) is called good if nums[i] == nums[j] and i < j.
Return the number of good pairs.
解题思路
- 枚举i j,进行比较
- 计算每个数出现的次数n,那么可以构成
N * (n - 1)/ 2
组
代码
class Solution:
def numIdenticalPairs(self, nums: List[int]) -> int:
n = len(nums)
res = 0
for i in range(n - 1):
for j in range(i + 1, n):
if nums[i] == nums[j]:
res += 1
return res
class Solution:
def numIdenticalPairs(self, nums: List[int]) -> int:
return sum(v * (v - 1) // 2 for v in Counter(nums).values())