1.设计LRU缓存结构
https://www.bilibili.com/video/BV1sb411v7Pt
思路:
1.链表和哈希表结合,建一个双向链表,存储key,value,后来的插入到链表尾部,有get操作的也插入到链表尾部,从链表头部删除数据。
2.用双向链表,双向链表可以定位到一个节点的前后,单链表不可以定位到链表前面的节点,只能定位到链表后面的节点。
#双向链表
class Listnode(object):
def __init__(self, key=None, value=None):
self.key = key
self.value = value
self.prev = None
self.next = None
class LRUCache(object):
def __init__(self, capacity):
"""
:type capacity: int
"""
self.maps={}
self.capacity=capacity
#新建一个双向链表,head和tail节点
self.head=ListNode()
self.tail=ListNode()
#连接起来
self.head.next=self.tail
self.tail.pre=self.head
def get(self, key):
"""
:type key: int
:rtype: int
"""
#如果在链表中,移动到链表头部
if key in self.maps:
node = self.maps[key]
#连接原节点左右
node.pre.next = node.next
node.next.pre = node.pre
#插入到头节点
node.pre = self.head
node.next = self.head.next
self.head.next.pre = node
self.head.next = node
res = self.maps.get(key, -1)
if res == -1:
return res
else:
return res.value
def put(self, key, value):
"""
:type key: int
:type value: int
:rtype: None
"""
#如果字典容量超过限制,先删除再插入数据。
newnode=Listnode(key,value)
if key not in self.maps and len(self.maps)>=self.capacity:
#删除链表尾部节点
lastnode=self.tail.pre
lastnode.pre.next = self.tail
self.tail.pre = lastnode.pre
#对应再map中删掉
self.maps.pop(lastnode.key)
if key in self.maps:
#删除原key value节点
node = self.maps[key]
#连接原节点左右
node.pre.next = node.next
node.next.pre = node.pre
#放入map中
self.maps[key]=newnode
#把链表插入到至头部,分两步,先连接node,再连接head
newnode.pre=self.head
newnode.next=self.head.next
self.head.next.pre=newnode
self.head.next=newnode
2.大数相乘
https://www.cnblogs.com/blfshiye/p/4282077.html
https://blog.csdn.net/u010983881/article/details/77503519
模拟小学乘法:最简单的乘法竖式手算的累加型;
思路:按照算数里的竖式乘法,逐位进行相加。相加用字符串相加的形式。
用weishu值来记录位数,相应的在后面补0。
字符串相加函数中,注意大于10的数要取一个//10的结果。
3.蓄水池抽样:链表随机节点
https://www.jianshu.com/p/7a9ea6ece2af
4.前缀二叉树
字典树,应用场景:搜索场景,输入g,提示google.
https://www.bilibili.com/video/BV1hA411t7kR?from=search&seid=10687107824110490892
5.python生成器的例子
6.手写一个稀疏矩阵相乘的代码
7.会议室2
lc 253