刷题1

1.设计LRU缓存结构

https://www.bilibili.com/video/BV1sb411v7Pt

https://leetcode-cn.com/problems/lru-cache/solution/shu-ju-jie-gou-fen-xi-python-ha-xi-shuang-xiang-li/

 

思路:

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.前缀二叉树

208. 实现 Trie (前缀树)

字典树,应用场景:搜索场景,输入g,提示google.

https://blog.csdn.net/qq_28327765/article/details/85124416?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase

https://www.bilibili.com/video/BV1hA411t7kR?from=search&seid=10687107824110490892

 

 

 

5.python生成器的例子

6.手写一个稀疏矩阵相乘的代码

7.会议室2 

lc 253

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值