Skip to content

Commit 7f596d2

Browse files
add 705
1 parent 35a7ec0 commit 7f596d2

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ Your ideas/fixes/algorithms are more than welcome!
100100
|713|[Subarray Product Less Than K](https://leetcode.com/problems/subarray-product-less-than-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_713.java) | O(n) | O(1) | |Medium |
101101
|712|[Minimum ASCII Delete Sum for Two Strings](https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_712.java) | O(m*n) | O(m*n) | |Medium | DP
102102
|709|[To Lower Case](https://leetcode.com/problems/to-lower-case/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_709.java) | O(n) | O(1) | |Easy| String
103+
|705|[Design HashSet](https://leetcode.com/problems/design-hashset/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_705.java) | O(1) | O(n) | |Easy| Design
103104
|704|[Binary Search](https://leetcode.com/problems/binary-search/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_704.java) | O(logn) | O(1) | |Easy| Binary Search
104105
|699|[Falling Squares](https://leetcode.com/problems/falling-squares/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_699.java) | O(n^2) | O(n) | |Hard | Segment Tree
105106
|698|[Partition to K Equal Sum Subsets](https://leetcode.com/problems/partition-to-k-equal-sum-subsets/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_698.java) | O(n*(2^n)) | O(2^n) | |Medium | Backtracking
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* 705. Design HashSet
8+
*
9+
* Design a HashSet without using any built-in hash table libraries.
10+
*
11+
* To be specific, your design should include these functions:
12+
*
13+
* add(value): Insert a value into the HashSet.
14+
* contains(value) : Return whether the value exists in the HashSet or not.
15+
* remove(value): Remove a value in the HashSet. If the value does not exist in the HashSet, do nothing.
16+
*
17+
* Example:
18+
*
19+
* MyHashSet hashSet = new MyHashSet();
20+
* hashSet.add(1);
21+
* hashSet.add(2);
22+
* hashSet.contains(1); // returns true
23+
* hashSet.contains(3); // returns false (not found)
24+
* hashSet.add(2);
25+
* hashSet.contains(2); // returns true
26+
* hashSet.remove(2);
27+
* hashSet.contains(2); // returns false (already removed)
28+
*
29+
* Note:
30+
*
31+
* All values will be in the range of [0, 1000000].
32+
* The number of operations will be in the range of [1, 10000].
33+
* Please do not use the built-in HashSet library.
34+
*/
35+
public class _705 {
36+
public static class Solution1 {
37+
class MyHashSet {
38+
Map<Integer, Integer> map;
39+
40+
/** Initialize your data structure here. */
41+
public MyHashSet() {
42+
map = new HashMap<>();
43+
}
44+
45+
public void add(int key) {
46+
map.put(key, 0);
47+
}
48+
49+
public void remove(int key) {
50+
if (map.containsKey(key)) {
51+
map.remove(key);
52+
}
53+
}
54+
55+
/** Returns true if this set contains the specified element */
56+
public boolean contains(int key) {
57+
return map.containsKey(key);
58+
}
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)