链表刷题总结

链表刷题总结



1.分配资源ID(华为2023.4.26笔试题)

1.1题目描述

给定一个管理ID的资源池,可以从资源池中分配资源ID和释放资源ID,分配方式有动态分配和指定分配,动态分配是从资源池的开始分配一个资源ID,指定分配是指定一个资源ID进行分配,无论哪种分配方式释放资源ID时都需要放到资源池的尾部。执行一系列操作后,请问资源池的第一个空闲资源ID应该是多少?
注意:

资源池的初始顺序是从小到大。

资源池中空闲资源ID不足时,动态分配失败,对资源池不进行任何操作.

指定分配资源ID已经被占用或者不在资源池范围内时,对资源池不进行任何操作。

释放资源ID不在资源池范围内时或者已经是空闲资源ID时,对资源池不进行任何操作。

保证每个用例最后都有空闲资源ID。

解答要求

时间限制: C/C++ 100ms,其他语言: 200ms

内存限制: C/C++ 32MB.其他语言: 64MB

输入

第一行是资源池的范围:

第二行是操作个数。

第三行开始,第一个数字代表操作类型,1表示动态分配,2表示指定分配,3表示释放;

如果第一个数字是1,第二个表示分配的个数;

如果第一个数字是2,第二个表示分配的资源ID;

如果第一个数字是3,第二个表示释放的资源ID。

输出

资源池的第一个空闲资源ID

1.2输入示例

样例1:

输入: 1 3
     3
     2 2
     3 2
     1 1
输出: 3
解释: 第一行资源池范围是[1.3],资源池的初始顺序是1->2->3。
第二行操作人数有3个。
第三行指定分配1个资源ID,资源ID是2,资源池中剩余的资源ID顺序是1->3->2.
第四行释放1个资源D,资源ID是2,资源池中剩余的资源ID顺序是1->3->2.
第五行动态分配1个资源ID,分配的资源ID是1,资源池中剩余的资源ID顺序是3->2。
执行以上操作后,资源池的第一个空闲资源ID是3。

1.3思路

哈希表+双向链表

1.4代码

python:

class Node:
    def __init__(self, val, pre=None, next=None) -> None:
        self.val = val
        self.pre = pre
        self.next = next

def func():
    start, end = list(map(int, input().split(' ')))
    head = Node(-1)
    tail = Node(-1)
    head.pre = tail
    head.next = tail
    tail.pre = head
    tail.next = head
    cur = head
    d = dict()
    cnt = 0
    for i in range(start, end+1):
        node = Node(i)
        d[i] = node
        cnt += 1
        cur.next = node
        node.pre = cur
        node.next = tail
        tail.pre = node
        cur = cur.next
   
    n = int(input())
    for _ in range(n):
        oper, num = list(map(int, input().split(' ')))
        if oper == 1:
            if cnt <= num:
                continue
            cur = head.next
            for i in range(num):
                del d[cur.val]
                nex = cur.next
                cur.pre.next = cur.next
                cur.next.pre = cur.pre
                cur = nex
            cnt -= num
        elif oper == 2:
            if num not in d:
                continue
            node = d[num]
            node.pre.next = node.next
            node.next.pre = node.pre
            cnt -= 1
            del d[num]
        else:
            if num in d:
                continue
            node = Node(num)
            d[num] = node
            tail.pre.next = node
            node.next = tail
            tail.pre = node
            cnt += 1
    print(head.next.val)

if __name__ == '__main__':
    func()

2.LRU缓存(Leecode146)

1.1题目描述

请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。
实现 LRUCache 类:
LRUCache(int capacity) 以 正整数 作为容量 capacity 初始化 LRU 缓存
int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。
void put(int key, int value) 如果关键字 key 已经存在,则变更其数据值 value ;如果不存在,则向缓存中插入该组 key-value 。如果插入操作导致关键字数量超过 capacity ,则应该 逐出 最久未使用的关键字。
函数 get 和 put 必须以 O(1) 的平均时间复杂度运行。

1.2输入示例

样例1输入:

输入
["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
输出
[null, null, null, 1, null, -1, null, -1, 3, 4]

解释
LRUCache lRUCache = new LRUCache(2);
lRUCache.put(1, 1); // 缓存是 {1=1}
lRUCache.put(2, 2); // 缓存是 {1=1, 2=2}
lRUCache.get(1);    // 返回 1
lRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {1=1, 3=3}
lRUCache.get(2);    // 返回 -1 (未找到)
lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3}
lRUCache.get(1);    // 返回 -1 (未找到)
lRUCache.get(3);    // 返回 3
lRUCache.get(4);    // 返回 4

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/lru-cache
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

1.3思路

哈希表+双向链表

1.4代码

python:

class DLinkedNode:
    def __init__(self, key = 0, val = 0) -> None:
        self.key = key
        self.val = val
        self.prev = None
        self.next = None

class LRUCache:

    def __init__(self, capacity: int):
        self.cache_dict = dict()
        self.head = DLinkedNode()
        self.tail = DLinkedNode()
        self.head.next = self.tail
        self.tail.prev = self.head
        self.size = 0
        self.capacity = capacity


    def get(self, key: int) -> int:
        if key in self.cache_dict:
            node = self.cache_dict[key]
            #将node移到表头
            node.prev.next = node.next
            node.next.prev = node.prev
            node.prev = self.head
            node.next = self.head.next
            self.head.next.prev = node
            self.head.next = node
            return node.val
        return -1


    def put(self, key: int, value: int) -> None:
        if key in self.cache_dict:
            node = self.cache_dict[key]
            node.val = value
            #删除原node
            node.prev.next = node.next
            node.next.prev = node.prev
            #将node放到表头
            self.head.next.prev = node
            node.prev = self.head
            node.next = self.head.next
            self.head.next = node
            self.cache_dict[key] = node
        else:
            if self.size < self.capacity:
                self.size += 1
                node = DLinkedNode(key, value)
                #将node放到表头
                self.head.next.prev = node
                node.prev = self.head
                node.next = self.head.next
                self.head.next = node
                self.cache_dict[key] = node
            else:
                node = DLinkedNode(key, value)
                #将node放到表头
                self.head.next.prev = node
                node.prev = self.head
                node.next = self.head.next
                self.head.next = node
                self.cache_dict[key] = node
                #删除表尾的元素
                last_node = self.tail.prev
                self.cache_dict.pop(last_node.key)
                last_node.prev.next = self.tail
                self.tail.prev = last_node.prev

总结

后期会不定期更新更多题解。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值