LeetCode Brush Questions 1512. The number of good pairs

LeetCode Brush Questions 1512. The number of good pairs

I don't know where I am going, but I am already on my way!
Time is hurried, although I have never met, but I met Yusi, it is really a great fate, thank you for your visit!
  • Subject :
    Give you an array of integers nums.
    If a set of numbers (i,j)meet nums[i] == nums[j], and i < jyou can think that this is a good set of number pairs.
    Returns the number of good pairs.
  • Example :
示例 1 :
输入:nums = [1,2,3,1,1,3]
输出:4
解释:有 4 组好数对,分别是 (0,3), (0,4), (3,4), (2,5) ,下标从 0 开始
示例 2 :
输入:nums = [1,1,1,1]
输出:6
解释:数组中的每组数字都是好数对
示例 3:
输入:nums = [1,2,3]
输出:0
  • Tips :
    • 1 <= nums.length <= 100
    • 1 <= nums[i] <= 100
  • Code:
class Solution:
    def numIdenticalPairs(self, nums: List[int]) -> int:
        count = 0
        for i in range(len(nums)-1):
            for j in range(i+1,len(nums)):
                if nums[i] ==nums[j]:
                    count += 1
        return count
# 执行用时:48 ms, 在所有 Python3 提交中击败了29.53%的用户
# 内存消耗:13.7 MB, 在所有 Python3 提交中击败了52.49%的用户
  • Algorithm description:
    Traverse all the data, count the number of tuples that meet the good number condition, and return the result.

Guess you like

Origin blog.csdn.net/qq_34331113/article/details/108298408