1748. 唯一元素的和
Solution
思路:看值域范围非常小,可以直接数组存值,就数组记录出现次数即可。
class Solution {
public int sumOfUnique(int[] nums) {
int len = nums.length;
int[] cnt = new int[100 + 1];
for (int i = 1; i <= 100; i++) {
cnt[i] = 0;
}
for (int i = 0; i < len; i++) {
cnt[nums[i]]++;
}
int sum = 0;
for (int i = 1; i <= 100; i++) {
if (cnt[i] == 1) {
sum += i;
}
}
return sum;
}
}