티스토리 뷰

leetcode.com/problems/sum-of-unique-elements/

 

Sum of Unique Elements - 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 value를 찾아서 그 값들의 합을 구하는 문제이다

class Solution {
public:
    int sumOfUnique(vector<int>& nums) {
        bool flag = false;
        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) {
                    flag = true;
                    break;
                }
            }
            if (flag == false) {
                cnt += nums[i];
            }
            else flag = false;
        }
        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
글 보관함