#706.设计哈希映射
class MyHashMap:
def _hash(self, key):
return key % self.keyRange
def __init__(self):
self.keyRange = 769
self.bucketArr = [Bucket() for i in range(self.keyRange)]
def put(self, key: int, value: int) -> None:
"""
value will always be non-negative.
"""
bucketIndex = self._hash(key)
self.bucketArr[bucketIndex].update((key, value))
def get(self, key: int) -> int:
"""
Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key
"""
bucketIndex = self._hash(key)
return self.bucketArr[bucketIndex].get(key)
def remove(self, key: int) -> None:
"""
Removes the mapping of the specified value key if this map contains a mapping for the key
"""
bucketIndex = self._hash(key)
del self.bucketArr[bucketIndex]
class Bucket(object):
def __init__(self):
self.bucket = []
def get(self, key):
for (k, v) in self.bucket:
if k == key:
return v
return -1
def update(self, key, value):
founded = False
for (k, v)in self.bucket:
if k == key:
if v != value:
self.bucket.append((key, value))
founded = True
return True
if not founded:
self.bucket.append((key, value))
def remove(self, key, value):
for (k, v) in enumerate(self.bucket):
if (key == k) and (value == v):
self.bucket.remove((key, value))
return True
return False
#705.设计哈希集合
class MyHashSet:
def __init__(self):
self.keyRange = 769
self.bucketArray = [Bucket() for i in range(self.keyRange)]
def _hash(self,key):
return key % self.keyRange
def add(self, key: int) -> None:
bucketIndex = self._hash(key)
self.bucketArray[bucketIndex].insert(key)
def remove(self, key: int) -> None:
bucketIndex = self._hash(key)
self.bucketArray[bucketIndex].remove(key)
def contains(self, key: int) -> bool:
"""
Returns true if this set contains the specified element
"""
bucketIndex = self._hash(key)
return self.bucketArray[bucketIndex].exist(key)
class Node(object):
"""docstring for Node"""
def __init__(self, value, nextNode = None):
self.value = value
self.next = nextNode
class Bucket(object):
def __init__(self):
self.head = Node(0)
def insert(self, newValue):
if not self.exist(newValue):
newNode = Node(newValue,self.head.next)
self.head.next = newNode
def remove(self, value):
pre = self.head
current = self.head.next
while current is not None:
if current.value == value:
pre.next = current.next
return
pre = pre.next
current = current.next
def exist(self, value):
current = self.head.next
while current is not None:
if current.value == value:
return True
current = current.next
return False
# 622. 设计循环队列
class MyCircularQueue:
def __init__(self, k: int):
"""
Initialize your data structure here. Set the size of the queue to be k.
"""
self.head = -1
self.tail = -1
self.size = k
self.data = [0] * k
def enQueue(self, value: int) -> bool:
"""
Insert an element into the circular queue. Return true if the operation is successful.
"""
if(self.isFull()): return False
elif(self.isEmpty()):self.head = 0
self.tail = (self.tail + 1) % self.size
self.data[self.tail] = value
return True
def deQueue(self) -> bool:
"""
Delete an element from the circular queue. Return true if the operation is successful.
"""
if(self.isEmpty()): return False
elif(self.head == self.tail):
self.head = self.tail = -1
return True
else:
self.head = (self.head + 1) % self.size
return True
def Front(self) -> int:
"""
Get the front item from the queue.
"""
if(self.isEmpty()): return -1
else:
return self.data[self.head]
def Rear(self) -> int:
"""
Get the last item from the queue.
"""
if (self.isEmpty()): return -1
else:
return self.data[self.tail]
def isEmpty(self) -> bool:
"""
Checks whether the circular queue is empty or not.
"""
if(self.head == -1): return True
def isFull(self) -> bool:
"""
Checks whether the circular queue is full or not.
"""
if((self.tail + 1) % self.size == self.head):
return True
else: return False
# 1.两数之和
def twoSum(self, nums: list[int], target: int) -> list[int]:
hashmap = {}
for index, num in enumerate(nums):
tmp = target - num
if tmp in hashmap:
return [hashmap[tmp],index]
hashmap[num] = index
return None
记录leetcode的python提交解答
猜你喜欢
转载自www.cnblogs.com/hanhande/p/12805875.html
今日推荐
周排行