Skip to content

Commit ed09f85

Browse files
authored
Create design-hashset.py
1 parent c680940 commit ed09f85

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

Python/design-hashset.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Time: O(1)
2+
# Space: O(n)
3+
4+
# Design a HashSet without using any built-in hash table libraries.
5+
#
6+
# To be specific, your design should include these two functions:
7+
#
8+
# add(value): Insert a value into the HashSet.
9+
# contains(value) : Return whether the value exists in the HashSet or not.
10+
# remove(value): Remove a value in the HashSet. If the value does not exist in the HashSet, do nothing.
11+
#
12+
# Example:
13+
#
14+
# MyHashSet hashSet = new MyHashSet();
15+
# hashSet.add(1);
16+
# hashSet.add(2);
17+
# hashSet.contains(1); // returns true
18+
# hashSet.contains(3); // returns false (not found)
19+
# hashSet.add(2);
20+
# hashSet.contains(2); // returns true
21+
# hashSet.remove(2);
22+
# hashSet.contains(2); // returns false (already removed)
23+
#
24+
# Note:
25+
# - All values will be in the range of [1, 1000000].
26+
# - The number of operations will be in the range of [1, 10000].
27+
# - Please do not use the built-in HashSet library.
28+
29+
30+
class ListNode(object):
31+
def __init__(self, key, val):
32+
self.val = val
33+
self.key = key
34+
self.next = None
35+
self.prev = None
36+
37+
38+
class LinkedList(object):
39+
def __init__(self):
40+
self.head = None
41+
self.tail = None
42+
43+
def insert(self, node):
44+
node.next, node.prev = None, None # avoid dirty node
45+
if self.head is None:
46+
self.head = node
47+
else:
48+
self.tail.next = node
49+
node.prev = self.tail
50+
self.tail = node
51+
52+
def delete(self, node):
53+
if node.prev:
54+
node.prev.next = node.next
55+
else:
56+
self.head = node.next
57+
if node.next:
58+
node.next.prev = node.prev
59+
else:
60+
self.tail = node.prev
61+
node.next, node.prev = None, None # make node clean
62+
63+
def find(self, key):
64+
curr = self.head
65+
while curr:
66+
if curr.key == key:
67+
break
68+
curr = curr.next
69+
return curr
70+
71+
72+
class MyHashSet(object):
73+
74+
def __init__(self):
75+
"""
76+
Initialize your data structure here.
77+
"""
78+
self.__data = [LinkedList() for _ in xrange(10000)]
79+
80+
def add(self, key):
81+
"""
82+
:type key: int
83+
:rtype: void
84+
"""
85+
l = self.__data[key % len(self.__data)]
86+
node = l.find(key)
87+
if not node:
88+
l.insert(ListNode(key, 0))
89+
90+
def remove(self, key):
91+
"""
92+
:type key: int
93+
:rtype: void
94+
"""
95+
l = self.__data[key % len(self.__data)]
96+
node = l.find(key)
97+
if node:
98+
l.delete(node)
99+
100+
def contains(self, key):
101+
"""
102+
Returns true if this set did not already contain the specified element
103+
:type key: int
104+
:rtype: bool
105+
"""
106+
l = self.__data[key % len(self.__data)]
107+
node = l.find(key)
108+
return node is not None
109+
110+
111+
# Your MyHashSet object will be instantiated and called as such:
112+
# obj = MyHashSet()
113+
# obj.add(key)
114+
# obj.remove(key)
115+
# param_3 = obj.contains(key)

0 commit comments

Comments
 (0)