반응형
https://leetcode.com/problems/number-of-good-pairs/description/
LeetCode - The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
각 수가 n개 나오면, (n*(n-1))/2개의 쌍을 만들 수 있습니다.
nums를 탐색하며 수의 개수를 세주고, 모든 쌍의 개수를 구해줍니다.
class Solution {
public:
int numIdenticalPairs(vector<int>& nums) {
int c[101] = {0};
int res = 0;
for(int n : nums) {
res += c[n]++;
}
return res;
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 1420. Build Array Where You Can Find The Maximum Exactly K Comparisons (0) | 2023.10.07 |
---|---|
LeetCode 706. Design HashMap (0) | 2023.10.04 |
LeetCode 557. Reverse Words in a String III (0) | 2023.10.01 |
LeetCode 389. Find the Difference (1) | 2023.09.25 |
LeetCode 392. Is Subsequence (0) | 2023.09.23 |