LeetCode 1748. 唯一元素的和
作者:互联网
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;
}
}
标签:nums,int,元素,Solution,1748,数组,LeetCode 来源: https://www.cnblogs.com/ACMerszl/p/15865650.html