Problem solving/Algorithms

[LeetCode] 1512. Number of Good Pairs (Python)

Young_A 2021. 1. 10. 09:38

LeetCode - Problems - Algorithms - 1512. Number of Good Pairs

Problem Description

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.

 

Example:

Constraints:

My Solution (Python)

class Solution(object):
    def numIdenticalPairs(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        answer = 0
        length = len(nums)
        for i in range(length):
            for j in range(i, length):
                if nums[i] == nums[j] and i < j:
                    answer += 1
        return answer