Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 532fba0

Browse files
add 1429
1 parent c915d76 commit 532fba0

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ _If you like this project, please leave me a star._ ★
298298
|1436|[Destination City](https://leetcode.com/problems/destination-city/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1436.java) | |Easy|String|
299299
|1432|[Max Difference You Can Get From Changing an Integer](https://leetcode.com/problems/max-difference-you-can-get-from-changing-an-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1432.java) | |Medium|String|
300300
|1431|[Kids With the Greatest Number of Candies](https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1431.java), [C++](../master/cpp/_1431.cpp) | |Easy|Array|
301+
|1429|[First Unique Number](https://leetcode.com/problems/first-unique-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1429.java) | |Medium|Array, HashTable, Design, Data Streams|
301302
|1428|[Leftmost Column with at Least a One](https://leetcode.com/problems/leftmost-column-with-at-least-a-one/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1428.java) | |Medium|Array|
302303
|1427|[Perform String Shifts](https://leetcode.com/problems/perform-string-shifts/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1427.java) | |Easy|Array, Math|
303304
|1426|[Counting Elements](https://leetcode.com/problems/counting-elements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1426.java) | |Easy|Array|
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashSet;
4+
import java.util.LinkedHashSet;
5+
import java.util.Set;
6+
7+
public class _1429 {
8+
public static class Solution1 {
9+
/**
10+
* Credit: https://leetcode.com/problems/first-unique-number/discuss/602698/Java-Easy-Set-O(1)
11+
* <p>
12+
* LinkedHashSet is a handy data structure to help preserve the order of all unique elements.
13+
*/
14+
public static class FirstUnique {
15+
16+
Set<Integer> uniqSet;
17+
Set<Integer> nonUniqSet;
18+
19+
public FirstUnique(int[] nums) {
20+
uniqSet = new LinkedHashSet<>();
21+
nonUniqSet = new HashSet<>();
22+
for (int num : nums) {
23+
add(num);
24+
}
25+
}
26+
27+
public int showFirstUnique() {
28+
if (!uniqSet.isEmpty()) {
29+
return uniqSet.iterator().next();
30+
}
31+
return -1;
32+
}
33+
34+
public void add(int value) {
35+
if (uniqSet.contains(value)) {
36+
uniqSet.remove(value);
37+
nonUniqSet.add(value);
38+
}
39+
if (!nonUniqSet.contains(value)) {
40+
uniqSet.add(value);
41+
}
42+
}
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)