最朴素的解法:

class Solution {
public:
    int sumOfUnique(vector<int>& nums) {
        int cnt[101] = {0},sum = 0;
        for(auto n:nums)
            cnt[n]++;

        for(int i=1;i < 101;i++)
        {
            if(cnt[i] == 1) sum += i;
        }
        return sum;
    }
};

运行效率:

Runtime: 4 ms, faster than 66.54% of C++ online submissions for Sum of Unique Elements.
Memory Usage: 7.8 MB, less than 92.94% of C++ online submissions for Sum of Unique Elements.

c++ stl解法:

class Solution {
public:
    int sumOfUnique(vector<int>& nums) {
        unordered_map<int,int> m;
        for(auto n:nums) m[n]++;
        
        auto lambda = [](int sum, const auto &p) {
            return sum + (p.second == 1 ? p.first : 0);
        };
        
        return accumulate(begin(m), end(m), 0, lambda);
    }
};

使用了lambda表达式用于求解只出现一次元素的累加和。