forked from super30admin/Design-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashset.py
More file actions
69 lines (54 loc) · 2.11 KB
/
Hashset.py
File metadata and controls
69 lines (54 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#// Time Complexity : Averge of O(1) unless everything ends up in one bucket.
#// Space Complexity : O(N)
#// Did this code successfully run on Leetcode : Yes
#// Any problem you faced while coding this : Add difficulty in converting add function concept to code.
#// Your code here along with comments explaining your approach
#Treating every number as a grid coordinate -> 2D (double hashing)
#Create the secondary bucket values when needed instead of ccreating at start
#Using boolean switch insted of actual values(True/false).
class MyHashSet(object):
def __init__(self):
self.bucket = 1000
self.items_per_bucket = 1000
self.storage = [None] * self.bucket
def primary_hash(self,key):
return key % self.bucket
def secondary_hash(self,key):
return key //self.items_per_bucket
def add(self, key):
"""
:type key: int
:rtype: None
"""
primary_index = self.primary_hash(key)
secondary_index = self.secondary_hash(key)
if self.storage[primary_index] is None:
if primary_index == 0:
size = self.items_per_bucket + 1
else:
size = self.items_per_bucket
self.storage[primary_index] = [False] * size
self.storage[primary_index][secondary_index] = True
def remove(self, key):
"""
:type key: int
:rtype: None
"""
primary_index = self.primary_hash(key)
secondary_index = self.secondary_hash(key)
#if bucket exist then only the key
if self.storage[primary_index] is not None:
self.storage[primary_index][secondary_index] = False
def contains(self, key):
"""
:type key: int
:rtype: bool
"""
primary_index = self.primary_hash(key)
secondary_index = self.secondary_hash(key)
return self.storage[primary_index] is not None and self.storage[primary_index][secondary_index]
# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)