-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesignHashSet.py
More file actions
121 lines (94 loc) · 3.32 KB
/
designHashSet.py
File metadata and controls
121 lines (94 loc) · 3.32 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/design-hashset/
# Author: Miao Zhang
# Date: 2021-03-03
class TreeNode:
def __init__(self, val: int):
self.val = val
self.left = None
self.right = None
class BSTree:
def __init__(self):
self.root = None
def _searchBST(self, root: TreeNode, val: int) -> TreeNode:
if root is None or val == root.val: return root
if root.val > val:
return self._searchBST(root.left, val)
else:
return self._searchBST(root.right, val)
def searchBST(self, val: int) -> TreeNode:
return self._searchBST(self.root, val)
def _insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
if not root:
return TreeNode(val)
if root.val > val:
root.left = self._insertIntoBST(root.left, val)
elif root.val < val:
root.right = self._insertIntoBST(root.right, val)
return root
def insertIntoBST(self, val: int) -> TreeNode:
return self._insertIntoBST(self.root, val)
def _deleteFromBST(self, root: TreeNode, val: int) -> TreeNode:
if not root: return None
if root.val > val:
root.left = self._deleteFromBST(root.left, val)
elif root.val < val:
root.right = self._deleteFromBST(root.right, val)
else:
if not root.left and not root.right:
root = None
elif root.right:
root.val = self.successor(root)
root.right = self._deleteFromBST(root.right, val)
else:
root.val = self.predecessor(root)
root.left = self._deleteFromBST(root.left, val)
return root
def successor(self, root: TreeNode) -> int:
root = root.right
while root.left:
root = root.left
return root.val
def predecessor(self, root: TreeNode) -> int:
root = root.left
while root.right:
root = root.right
return root.val
def deleteFromBST(self, val: int) -> TreeNode:
return self._deleteFromBST(self.root, val)
class Bucket:
def __init__(self):
self.tree = BSTree()
def insert(self, val: int) -> None:
self.tree.root = self.tree.insertIntoBST(val)
def delete(self, val: int) -> None:
self.tree.root = self.tree.deleteFromBST(val)
def contains(self, val: int) -> bool:
return (self.tree.searchBST(val) is not None)
class MyHashSet:
def __init__(self):
"""
Initialize your data structure here.
"""
self.keyRange = 769
self.bucket = [Bucket() for i in range(self.keyRange)]
def _hash(self, key: int) -> int:
return key % self.keyRange
def add(self, key: int) -> None:
idx = self._hash(key)
self.bucket[idx].insert(key)
def remove(self, key: int) -> None:
idx = self._hash(key)
self.bucket[idx].delete(key)
def contains(self, key: int) -> bool:
"""
Returns true if this set contains the specified element
"""
idx = self._hash(key)
return self.bucket[idx].contains(key)
# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)