-
Notifications
You must be signed in to change notification settings - Fork 0
/
1748.cpp
37 lines (35 loc) · 911 Bytes
/
1748.cpp
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
32
33
34
35
36
37
/*给你一个整数数组 nums 。数组中唯一元素是那些只出现 恰好一次 的元素。
请你返回 nums 中唯一元素的 和 。*/
#include<iostream>
#include<unordered_map>
#include<vector>
using namespace std;
class Solution {
public:
int sumOfUnique(vector<int>& nums) {
unordered_map<int,int>all_nums;
for(auto i:nums){
all_nums[i]=0;
}
for(auto i:nums){
all_nums[i]++;
}
int answer=0;
for(auto item:all_nums){
if(item.second==1){
answer+=item.first;
}
}
return answer;
}
};
int main(){
Solution a;
vector<int>b={1,2,3,2};
vector<int>c={1,1,1,1,1};
vector<int>d={1,2,3,4,5};
cout<<a.sumOfUnique(b)<<endl;
cout<<a.sumOfUnique(c)<<endl;
cout<<a.sumOfUnique(d)<<endl;
return 0;
}