Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed Design 2 #2030

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions Sample.java

This file was deleted.

52 changes: 52 additions & 0 deletions design_hashmap_706.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class ListNode:
def __init__(self, key, value):
self.key = key
self.value = value
self.next = None

class MyHashMap:

def __init__(self):
self.size = 1000
self.table = [None] * self.size

def _index(self, key: int) -> int:
return key % self.size

def put(self, key: int, value: int) -> None:
idx = self._index(key)
if not self.table[idx]:
self.table[idx] = ListNode(key, value)
return
current = self.table[idx]
while current:
if current.key == key:
current.value = value
return
if not current.next:
current.next = ListNode(key, value)
return
current = current.next

def get(self, key: int) -> int:
idx = self._index(key)
current = self.table[idx]
while current:
if current.key == key:
return current.value
current = current.next
return -1

def remove(self, key: int) -> None:
idx = self._index(key)
current = self.table[idx]
if not current:
return
if current.key == key:
self.table[idx] = current.next
return
while current.next:
if current.next.key == key:
current.next = current.next.next
return
current = current.next
20 changes: 20 additions & 0 deletions min_stack_155.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class MinStack:

def __init__(self):
self.s = []
self.minstack = []

def push(self, val: int) -> None:
self.s.append(val)
min_val = min(val, self.minstack[-1] if self.minstack else val)
self.minstack.append(min_val)

def pop(self) -> None:
self.s.pop()
self.minstack.pop()

def top(self) -> int:
return self.s[-1]

def getMin(self) -> int:
return self.minstack[-1]
26 changes: 26 additions & 0 deletions queue_using_stack_232.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class MyQueue:

def __init__(self):
self.stack1 = []
self.stack2 = []

def push(self, x: int) -> None:
self.stack1.append(x)
while self.stack1:
self.stack2.append(self.stack1.pop())

def pop(self) -> int:
return self.stack2.pop(0)

def peek(self) -> int:
return self.stack2[0]

def empty(self) -> bool:
return not self.stack2

# 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()