LeetCode (1512) - the number of good number pairs

table of Contents

topic

1512. The number of good number pairs
gives you an integer array nums.

If a set of numbers (i, j) satisfies nums[i] == nums[j] and i <j, it can be considered a good number pair.

Returns the number of good pairs.

Example 1:

Input: nums = [1,2,3,1,1,3]
Output: 4
Explanation: There are 4 good number pairs, namely (0,3), (0,4), (3,4),( 2,5), the subscript starts from 0

Example 2:

Input: nums = [1,1,1,1]
Output: 6
Explanation: Each group of numbers in the array is a good number pair

Example 3:

Input: nums = [1,2,3]
Output: 0

prompt:

1 <= nums.length <= 100
1 <= nums[i] <= 100

Problem solution (Java)

Two for loops to solve the problem

class Solution {
    
    
    public int numIdenticalPairs(int[] nums) 
    {
    
    
        int count = 0;
        for(int index = 0;index < nums.length;index++)
        {
    
    
            for(int scan = index + 1;scan < nums.length;scan++)
            {
    
    
                if(nums[index] == nums[scan])
                {
    
    
                    count++;
                }
            }
        }
        return count;
    }
}

Guess you like

Origin blog.csdn.net/weixin_46841376/article/details/113880952