Skip to content

Commit 93f0475

Browse files
committed
feat: add solutions to leetcode problem: No.1748. Sum of Unique Elements
1 parent 0799a56 commit 93f0475

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed

solution/1700-1799/1748.Sum of Unique Elements/README.md

+25-3
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,49 @@
4242
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
4343
</ul>
4444

45-
4645
## 解法
4746

4847
<!-- 这里可写通用的实现逻辑 -->
4948

49+
定义一个计数器 counter,存放数组每个元素出现的次数。
50+
51+
然后遍历 counter 中每个元素,累加次数为 1 的所有下标即可。
52+
5053
<!-- tabs:start -->
5154

5255
### **Python3**
5356

5457
<!-- 这里可写当前语言的特殊实现逻辑 -->
5558

5659
```python
57-
60+
class Solution:
61+
def sumOfUnique(self, nums: List[int]) -> int:
62+
counter = [0] * 101
63+
for num in nums:
64+
counter[num] += 1
65+
return sum([i for i in range(1, 101) if counter[i] == 1])
5866
```
5967

6068
### **Java**
6169

6270
<!-- 这里可写当前语言的特殊实现逻辑 -->
6371

6472
```java
65-
73+
class Solution {
74+
public int sumOfUnique(int[] nums) {
75+
int[] counter = new int[101];
76+
for (int num : nums) {
77+
++counter[num];
78+
}
79+
int res = 0;
80+
for (int i = 1; i < 101; ++i) {
81+
if (counter[i] == 1) {
82+
res += i;
83+
}
84+
}
85+
return res;
86+
}
87+
}
6688
```
6789

6890
### **...**

solution/1700-1799/1748.Sum of Unique Elements/README_EN.md

+21-3
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,39 @@
4141
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
4242
</ul>
4343

44-
4544
## Solutions
4645

4746
<!-- tabs:start -->
4847

4948
### **Python3**
5049

5150
```python
52-
51+
class Solution:
52+
def sumOfUnique(self, nums: List[int]) -> int:
53+
counter = [0] * 101
54+
for num in nums:
55+
counter[num] += 1
56+
return sum([i for i in range(1, 101) if counter[i] == 1])
5357
```
5458

5559
### **Java**
5660

5761
```java
58-
62+
class Solution {
63+
public int sumOfUnique(int[] nums) {
64+
int[] counter = new int[101];
65+
for (int num : nums) {
66+
++counter[num];
67+
}
68+
int res = 0;
69+
for (int i = 1; i < 101; ++i) {
70+
if (counter[i] == 1) {
71+
res += i;
72+
}
73+
}
74+
return res;
75+
}
76+
}
5977
```
6078

6179
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int sumOfUnique(int[] nums) {
3+
int[] counter = new int[101];
4+
for (int num : nums) {
5+
++counter[num];
6+
}
7+
int res = 0;
8+
for (int i = 1; i < 101; ++i) {
9+
if (counter[i] == 1) {
10+
res += i;
11+
}
12+
}
13+
return res;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def sumOfUnique(self, nums: List[int]) -> int:
3+
counter = [0] * 101
4+
for num in nums:
5+
counter[num] += 1
6+
return sum([i for i in range(1, 101) if counter[i] == 1])

0 commit comments

Comments
 (0)