class ListNode:
def __init__(self, key=None, value=None):
self.key = key
self.value = value
self.prev = None
self.next = None
class LRUCache:
def __init__(self, capacity):
self.capacity = capacity
self.hashmap = {}
# 新建两个节点 head 和 tail
self.head = ListNode()
self.tail = ListNode()
# 初始化链表为 head <-> tail
self.head.next = self.tail
self.tail.prev = self.head
# 因为get与put操作都可能需要将双向链表中的某个节点移到头部(变成最新访问的),所以定义一个方法
def move_node_to_header(self, key):
# 先将哈希表key指向的节点拎出来,为了简洁起名node
# hashmap[key] hashmap[key]
# | |
# V --> V
# prev <-> node <-> next pre <-> next ... node
node = self.hashmap[key]
node.prev.next = node.next
node.next.prev = node.prev
# 之后将node插入到头部节点前
# hashmap[key] hashmap[key]
# | |
# V --> V
# header <-> next ... node header <-> node <-> next
node.prev = self.head
node.next = self.head.next
self.head.next.prev = node
self.head.next = node
def add_node_to_header(self, key, value):
new = ListNode(key, value)
self.hashmap[key] = new
new.prev = self.head
new.next = self.head.next
self.head.next.prev = new
self.head.next = new
def pop_tail(self):
last_node = self.tail.prev
# 去掉链表尾部的节点在哈希表的对应项
self.hashmap.pop(last_node.key)
# 去掉最久没有被访问过的节点,即尾部Tail之前的一个节点
last_node.prev.next = self.tail
self.tail.prev = last_node.prev
return last_node
def get(self, key):
if key in self.hashmap:
# 如果已经在链表中了久把它移到头部(变成最新访问的)
self.move_node_to_header(key)
res = self.hashmap.get(key, -1)
if res == -1:
return res
else:
return res.value
def put(self, key, value):
if key in self.hashmap:
# 如果key本身已经在哈希表中了就不需要在链表中加入新的节点
# 但是需要更新字典该值对应节点的value
self.hashmap[key].value = value
# 之后将该节点移到链表头部
self.move_node_to_header(key)
else:
if len(self.hashmap) >= self.capacity:
# 若cache容量已满,删除cache中最不常用的节点
self.pop_tail()
self.add_node_to_header(key,value)
最小堆
主要的方法参考labuladong的二叉堆详解实现优先级队列
class MinStack(object):
def __init__(self):
"""
initialize your data structure here.
"""
self.minStack = [None]
self.n = 0
def parent(self, k):
return k // 2
def left(self, k):
return 2 * k
def right(self, k):
return 2 * k + 1
def less(self, i, j):
return self.minStack[i] < self.minStack[j]
def sink(self, k):
while(self.left(k) <= self.n):
# 找到最小值
smaller = self.left(k)
if (self.right(k) <= self.n and self.less(self.right(k), smaller)):
smaller = self.right(k)
if self.less(k, smaller):
break
# 交换数值
tmp = self.minStack[smaller]
self.minStack[smaller] = self.minStack[k]
self.minStack[k] = tmp
# 更改k
k = smaller
def swim(self, k):
while(self.parent(k) >= 1 and self.less(k, self.parent(k))):
# 交换数值
tmp = self.minStack[self.parent(k)]
self.minStack[self.parent(k)] = self.minStack[k]
self.minStack[k] = tmp
# 更改k
k = self.parent(k)
def insert(self, x):
"""
:type x: int
:rtype: None
"""
# 在stack的尾部加入元素
self.n += 1
self.minStack.append(x)
# 上浮到合适的位置
self.swim(self.n)
def delMin(self):
"""
:rtype: None
"""
tmp = self.minStack[1]
self.minStack[1] = self.minStack[self.n]
self.minStack[self.n] = tmp
self.n -= 1
self.sink(1)
def getMin(self):
"""
:rtype: int
"""
if self.n >= 1:
return self.minStack[1]
class MinStack:
# 辅助栈和数据栈同步
# 思路简单不容易出错
def __init__(self):
# 两个栈的长度相同
# 数据栈, 就是正常的栈结构
self.data = []
# 辅助栈, 里面存放的的是和data每个阶段对应的最小值,是同步的
self.helper = []
def push(self, x):
self.data.append(x)
# x比helper栈顶元素小, 加入栈顶成为当前阶段的最小元素
if len(self.helper) == 0 or x <= self.helper[-1]:
self.helper.append(x)
else:
# x没有比站内的最下元素小, 所以将self.helper[-1]加入helper加入栈顶
self.helper.append(self.helper[-1])
def pop(self):
if self.data:
# 两站同步
self.helper.pop()
return self.data.pop()
def top(self):
if self.data:
return self.data[-1]
def getMin(self):
if self.helper:
return self.helper[-1]