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 #2045

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
64 changes: 64 additions & 0 deletions hashmap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
class ListNode:
def __init__(self, key: int, value: int):
self.key = key
self.value = value
self.next = None

class MyHashMap:

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

def hashFunction(self, val):
return val % self.size

def put(self, key: int, value: int) -> None:
hashVal = self.hashFunction(key)

if self.hashTable[hashVal] == None:
self.hashTable[hashVal] = ListNode(key, value)
return
else:
curr = self.hashTable[hashVal]
prev = None
while curr:
if curr.key == key:
curr.value = value
return
prev = curr
curr = curr.next

if prev:
prev.next = ListNode(key, value)
return

def get(self, key: int) -> int:
hashVal = self.hashFunction(key)
curr = self.hashTable[hashVal]

while curr:
if curr.key == key:
return curr.value
curr = curr.next
return -1

def remove(self, key: int) -> None:
hashVal = self.hashFunction(key)
curr = self.hashTable[hashVal]
prev = None
if curr == None:
return
if curr.key == key:
self.hashTable[hashVal] = curr.next
curr.next = None
return
while curr:
if curr.key == key:
prev.next = curr.next
curr.next = None
return

prev = curr
curr = curr.next
return
23 changes: 23 additions & 0 deletions queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class MyQueue:

def __init__(self):
self.instack = []
self.outstack = []

def push(self, x: int) -> None:
self.instack.append(x)

def pop(self) -> int:
if len(self.outstack) == 0:
while len(self.instack) > 0:
self.outstack.append(self.instack.pop())
return self.outstack.pop() if self.outstack else None

def peek(self) -> int:
if len(self.outstack) == 0:
while len(self.instack) > 0:
self.outstack.append(self.instack.pop())
return self.outstack[-1] if self.outstack else None

def empty(self) -> bool:
return len(self.outstack) == 0 and len(self.instack) == 0