Skip to content

Commit 95d669b

Browse files
committed
feat: add solutions to lc problem: No.1429. First Unique Number
1 parent 7458a3a commit 95d669b

File tree

4 files changed

+206
-6
lines changed

4 files changed

+206
-6
lines changed

solution/1400-1499/1429.First Unique Number/README.md

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,27 +82,94 @@ firstUnique.showFirstUnique(); // 返回 -1
8282
<li>最多调用 <code>5000</code> 次 <code>showFirstUnique</code> 和 <code>add</code> 。</li>
8383
</ul>
8484

85-
8685
## 解法
8786

8887
<!-- 这里可写通用的实现逻辑 -->
8988

89+
“有序哈希表”实现。
90+
9091
<!-- tabs:start -->
9192

9293
### **Python3**
9394

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

9697
```python
97-
98+
class FirstUnique:
99+
100+
def __init__(self, nums: List[int]):
101+
self.counter = collections.OrderedDict()
102+
self.unique_nums = collections.OrderedDict()
103+
for num in nums:
104+
self.counter[num] = self.counter.get(num, 0) + 1
105+
for k, v in self.counter.items():
106+
if v == 1:
107+
self.unique_nums[k] = 1
108+
109+
def showFirstUnique(self) -> int:
110+
if len(self.unique_nums) == 0:
111+
return -1
112+
for k in self.unique_nums.keys():
113+
return k
114+
115+
def add(self, value: int) -> None:
116+
if value not in self.counter:
117+
self.counter[value] = 1
118+
self.unique_nums[value] = 1
119+
else:
120+
self.counter[value] += 1
121+
if value in self.unique_nums:
122+
self.unique_nums.pop(value)
123+
124+
# Your FirstUnique object will be instantiated and called as such:
125+
# obj = FirstUnique(nums)
126+
# param_1 = obj.showFirstUnique()
127+
# obj.add(value)
98128
```
99129

100130
### **Java**
101131

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

104134
```java
105-
135+
class FirstUnique {
136+
private Map<Integer, Integer> counter;
137+
private Set<Integer> uniqueNums;
138+
139+
public FirstUnique(int[] nums) {
140+
counter = new LinkedHashMap<>();
141+
uniqueNums = new LinkedHashSet<>();
142+
for (int num : nums) {
143+
counter.put(num, counter.getOrDefault(num, 0) + 1);
144+
}
145+
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
146+
if (entry.getValue() == 1) {
147+
uniqueNums.add(entry.getKey());
148+
}
149+
}
150+
}
151+
152+
public int showFirstUnique() {
153+
return uniqueNums.isEmpty() ? -1 : uniqueNums.iterator().next();
154+
}
155+
156+
public void add(int value) {
157+
if (!counter.containsKey(value)) {
158+
counter.put(value, 1);
159+
uniqueNums.add(value);
160+
} else {
161+
counter.put(value, counter.get(value) + 1);
162+
uniqueNums.remove(value);
163+
}
164+
}
165+
}
166+
167+
/**
168+
* Your FirstUnique object will be instantiated and called as such:
169+
* FirstUnique obj = new FirstUnique(nums);
170+
* int param_1 = obj.showFirstUnique();
171+
* obj.add(value);
172+
*/
106173
```
107174

108175
### **...**

solution/1400-1499/1429.First Unique Number/README_EN.md

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,86 @@ firstUnique.showFirstUnique(); // return -1
7878
<li>At most <code>50000</code>&nbsp;calls will be made to <code>showFirstUnique</code>&nbsp;and <code>add</code>.</li>
7979
</ul>
8080

81-
8281
## Solutions
8382

8483
<!-- tabs:start -->
8584

8685
### **Python3**
8786

8887
```python
89-
88+
class FirstUnique:
89+
90+
def __init__(self, nums: List[int]):
91+
self.counter = collections.OrderedDict()
92+
self.unique_nums = collections.OrderedDict()
93+
for num in nums:
94+
self.counter[num] = self.counter.get(num, 0) + 1
95+
for k, v in self.counter.items():
96+
if v == 1:
97+
self.unique_nums[k] = 1
98+
99+
def showFirstUnique(self) -> int:
100+
if len(self.unique_nums) == 0:
101+
return -1
102+
for k in self.unique_nums.keys():
103+
return k
104+
105+
def add(self, value: int) -> None:
106+
if value not in self.counter:
107+
self.counter[value] = 1
108+
self.unique_nums[value] = 1
109+
else:
110+
self.counter[value] += 1
111+
if value in self.unique_nums:
112+
self.unique_nums.pop(value)
113+
114+
# Your FirstUnique object will be instantiated and called as such:
115+
# obj = FirstUnique(nums)
116+
# param_1 = obj.showFirstUnique()
117+
# obj.add(value)
90118
```
91119

92120
### **Java**
93121

94122
```java
95-
123+
class FirstUnique {
124+
private Map<Integer, Integer> counter;
125+
private Set<Integer> uniqueNums;
126+
127+
public FirstUnique(int[] nums) {
128+
counter = new LinkedHashMap<>();
129+
uniqueNums = new LinkedHashSet<>();
130+
for (int num : nums) {
131+
counter.put(num, counter.getOrDefault(num, 0) + 1);
132+
}
133+
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
134+
if (entry.getValue() == 1) {
135+
uniqueNums.add(entry.getKey());
136+
}
137+
}
138+
}
139+
140+
public int showFirstUnique() {
141+
return uniqueNums.isEmpty() ? -1 : uniqueNums.iterator().next();
142+
}
143+
144+
public void add(int value) {
145+
if (!counter.containsKey(value)) {
146+
counter.put(value, 1);
147+
uniqueNums.add(value);
148+
} else {
149+
counter.put(value, counter.get(value) + 1);
150+
uniqueNums.remove(value);
151+
}
152+
}
153+
}
154+
155+
/**
156+
* Your FirstUnique object will be instantiated and called as such:
157+
* FirstUnique obj = new FirstUnique(nums);
158+
* int param_1 = obj.showFirstUnique();
159+
* obj.add(value);
160+
*/
96161
```
97162

98163
### **...**
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class FirstUnique {
2+
private Map<Integer, Integer> counter;
3+
private Set<Integer> uniqueNums;
4+
5+
public FirstUnique(int[] nums) {
6+
counter = new LinkedHashMap<>();
7+
uniqueNums = new LinkedHashSet<>();
8+
for (int num : nums) {
9+
counter.put(num, counter.getOrDefault(num, 0) + 1);
10+
}
11+
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
12+
if (entry.getValue() == 1) {
13+
uniqueNums.add(entry.getKey());
14+
}
15+
}
16+
}
17+
18+
public int showFirstUnique() {
19+
return uniqueNums.isEmpty() ? -1 : uniqueNums.iterator().next();
20+
}
21+
22+
public void add(int value) {
23+
if (!counter.containsKey(value)) {
24+
counter.put(value, 1);
25+
uniqueNums.add(value);
26+
} else {
27+
counter.put(value, counter.get(value) + 1);
28+
uniqueNums.remove(value);
29+
}
30+
}
31+
}
32+
33+
/**
34+
* Your FirstUnique object will be instantiated and called as such:
35+
* FirstUnique obj = new FirstUnique(nums);
36+
* int param_1 = obj.showFirstUnique();
37+
* obj.add(value);
38+
*/
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class FirstUnique:
2+
3+
def __init__(self, nums: List[int]):
4+
self.counter = collections.OrderedDict()
5+
self.unique_nums = collections.OrderedDict()
6+
for num in nums:
7+
self.counter[num] = self.counter.get(num, 0) + 1
8+
for k, v in self.counter.items():
9+
if v == 1:
10+
self.unique_nums[k] = 1
11+
12+
def showFirstUnique(self) -> int:
13+
if len(self.unique_nums) == 0:
14+
return -1
15+
for k in self.unique_nums.keys():
16+
return k
17+
18+
def add(self, value: int) -> None:
19+
if value not in self.counter:
20+
self.counter[value] = 1
21+
self.unique_nums[value] = 1
22+
else:
23+
self.counter[value] += 1
24+
if value in self.unique_nums:
25+
self.unique_nums.pop(value)
26+
27+
# Your FirstUnique object will be instantiated and called as such:
28+
# obj = FirstUnique(nums)
29+
# param_1 = obj.showFirstUnique()
30+
# obj.add(value)

0 commit comments

Comments
 (0)