티스토리 뷰

leetcode.com/problems/n-repeated-element-in-size-2n-array/

 

N-Repeated Element in Size 2N Array - LeetCode

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

배열을 argument로 넘겨준다.

해당 배열 내 반복되는 숫자가 있다. 그 외의 숫자는 unique 값이다. 반복되는 숫자를 return

class Solution {
public:
    int repeatedNTimes(vector<int>& A) {
        int cnt = 0;
        bool flag = false;
        for (int i = 0; i < A.size(); i++) {
            for (int j = 0; j < A.size(); j++) {
                if (i != j && A[i] == A[j]) {
                    flag = true;
                    break;
                }
            }
            if (flag == true) {
                cnt = A[i];
                break;
            }
        }
        return cnt;
    }
};
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함