1512. The number of good pairs (C++)
1 topic description
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.
2 Example description
2.1 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
2.2 Example 2
Input: accounts = [[1,5],[7,3],[3,5]]
Output: 10
Explanation:
The total assets of the
first customer = 6 The total assets of the second customer = 10
The third Total assets of each customer = 8
The second customer is the richest, total assets of 10
2.3 Example 3
Input: nums = [1,1,1,1]
Output: 6
Explanation: Each group of numbers in the array is a good number pair
3 Problem solving tips
Input: nums = [1,2,3]
Output: 0
4 Detailed source code (C++)
class Solution {
public:
int numIdenticalPairs(vector<int>& nums) {
int count = 0;
for (int i = 0 ; i < nums.size() - 1 ; i++)
{
for (int j = i + 1 ; j < nums.size() ; j++)
{
if (nums[i] == nums[j])
{
count ++;
}
}
}
return count;
}
};