본문 바로가기

Algorithm/Implementation

(C++) - LeetCode (easy) 1507. Reformat Date

반응형

https://leetcode.com/problems/number-of-good-pairs/

📕 풀이방법

📔 입력 및 초기화

정답 변수 cnt를 선언 후 0으로 초기화해줍니다.

📔 풀이과정

nums의 원소를 순회하며 조건에 맞는 경우 cnt를 1씩 증가시켜줍니다.

📔 정답 출력 | 반환

cnt를 반환합니다.


📕 Code

📔 C++

class Solution {
public:
    int numIdenticalPairs(vector<int>& nums) {
        int cnt = 0;
        for(int i = 0; i < nums.size(); i++) {
            for(int j = 0; j < nums.size(); j++) {
                if(nums[i] == nums[j] && i < j) cnt++;
            }
        }
        return cnt;
    }
};

*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.