diff --git a/HashSet.py b/HashSet.py new file mode 100644 index 00000000..6c95aef7 --- /dev/null +++ b/HashSet.py @@ -0,0 +1,37 @@ +class MyHashSet(object): + + def __init__(self): + """ + Initialize your data structure here. + """ + self.set=[0]*1000000 + + def add(self, key): + """ + :type key: int + :rtype: None + """ + self.set[key]=1 + + def remove(self, key): + """ + :type key: int + :rtype: None + """ + + self.set[key]=0 + + def contains(self, key): + """ + Returns true if this set contains the specified element + :type key: int + :rtype: bool + """ + return self.set[key]==1 + + +# Your MyHashSet object will be instantiated and called as such: +# obj = MyHashSet() +# obj.add(key) +# obj.remove(key) +# param_3 = obj.contains(key) \ No newline at end of file diff --git a/ImplementQueue.py b/ImplementQueue.py new file mode 100644 index 00000000..cd1c9575 --- /dev/null +++ b/ImplementQueue.py @@ -0,0 +1,58 @@ +class MyQueue(object): + + def __init__(self): + """ + Initialize your data structure here. + """ + self.stackin=[] + self.stackout=[] + + def push(self, x): + """ + Push element x to the back of queue. + :type x: int + :rtype: None + """ + self.stackin.append(x) + + def pop(self): + """ + Removes the element from in front of queue and returns that element. + :rtype: int + """ + if len(self.stackout)!=0: + return self.stackout.pop() + else: + while len(self.stackin)!=0: + self.stackout.append(self.stackin.pop()) + return self.stackout.pop() + + def peek(self): + """ + Get the front element. + :rtype: int + """ + if len(self.stackout)!=0: + return self.stackout[-1] + else: + while len(self.stackin)!=0: + self.stackout.append(self.stackin.pop()) + return self.stackout[-1] + + def empty(self): + """ + Returns whether the queue is empty. + :rtype: bool + """ + if len(self.stackin)==0 and len(self.stackout)==0: + return True + else: + return False + + +# Your MyQueue object will be instantiated and called as such: +# obj = MyQueue() +# obj.push(x) +# param_2 = obj.pop() +# param_3 = obj.peek() +# param_4 = obj.empty() \ No newline at end of file