Python|每日一练|单选记录:LRU 缓存机制|排序链表|搜索二维矩阵

1、LRU 缓存机制

运用你所掌握的数据结构,设计和实现一个  LRU (最近最少使用) 缓存机制(https://baike.baidu.com/item/LRU) 

实现 LRUCache 类:

  • LRUCache(int capacity) 以正整数作为容量 capacity 初始化 LRU 缓存
  • int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 
  • void put(int key, int value) 如果关键字已经存在,则变更其数据值;如果关键字不存在,则插入该组「关键字-值」。当缓存容量达到上限时,它应该在写入新数据之前删除最久未使用的数据值,从而为新的数据值留出空间。

 

进阶:你是否可以在 O(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

 

提示:

  • 1 <= capacity <= 3000
  • 0 <= key <= 10000
  • 0 <= value <= 105
  • 最多调用 2 * 105  get  put

示例代码:

class LRUCache:
    def __init__(self, capacity):
        """
        :type capacity: int
        """
        self.maxlength = capacity
        self.array = {}
        self.array_list = []
    def get(self, key):
        """
        :type key: int
        :rtype: int
        """
        value = self.array.get(key)
        if value is not None and self.array_list[0] is not key:
            index = self.array_list.index(key)
            self.array_list.pop(index)
            self.array_list.insert(0, key)
        value = value if value is not None else -1
        return value
    def put(self, key, value):
        """
        :type key: int
        :type value: int
        :rtype: void
        """
        if self.array.get(key) is not None:
            index = self.array_list.index(key)
            self.array.pop(key)
            self.array_list.pop(index)
        if len(self.array_list) >= self.maxlength:
            key_t = self.array_list.pop(self.maxlength - 1)
            self.array.pop(key_t)
        self.array[key] = value
        self.array_list.insert(0, key)

2、排序链表

排序链表

给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表 

进阶:

  • 你可以在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序吗?

 

示例 1

https://i-blog.csdnimg.cn/blog_migrate/43c110a1f0d8f715d2b47af03e25608c.jpeg

输入:head = [4,2,1,3]

输出:[1,2,3,4]

示例 2

https://i-blog.csdnimg.cn/blog_migrate/837c97b3ec87935a303c55473f4a5791.jpeg

输入:head = [-1,5,3,4,0]

输出:[-1,0,3,4,5]

示例 3

输入:head = []

输出:[]

提示:

  • 链表中节点的数目在范围 [0, 5 * 104] 
  • -105 <= Node.val <= 105

示例代码:

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None
class Solution:
    def sortList(self, head: ListNode) -> ListNode:
        if head == None:
            return None
        else:
            return self.mergeSort(head)
    def mergeSort(self, head):
        if head.next == None:
            return head
        fast = head
        slow = head
        pre = None
        while fast != None and fast.next != None:
            pre = slow
            slow = slow.next
            fast = fast.next.next
        pre.next = None
        left = self.mergeSort(head)
        right = self.mergeSort(slow)
        return self.merge(left, right)
    def merge(self, left, right):
        tempHead = ListNode(0)
        cur = tempHead
        while left != None and right != None:
            if left.val <= right.val:
                cur.next = left
                cur = cur.next
                left = left.next
            else:
                cur.next = right
                cur = cur.next
                right = right.next
        if left != None:
            cur.next = left
        if right != None:
            cur.next = right
        return tempHead.next

3、搜索二维矩阵

编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性:

  • 每行中的整数从左到右按升序排列。
  • 每行的第一个整数大于前一行的最后一个整数。

 

示例 1

https://i-blog.csdnimg.cn/blog_migrate/a3f1d67232e798713daa55fb27a58562.jpeg

输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
输出:true

示例 2

https://i-blog.csdnimg.cn/blog_migrate/01cb375643c0f89bd96f667a7c6aed8b.jpeg

输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
输出:false

提示:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 100
  • -104 <= matrix[i][j], target <= 104

示例代码:

class Solution(object):
    def searchMatrix(self, matrix, target):
        """
        :type matrix: List[List[int]]
        :type target: int
        :rtype: bool
        """
        if not matrix or not matrix[0]:
            return False
        rows = len(matrix)
        cols = len(matrix[0])
        row, col = 0, cols - 1
        while True:
            if row < rows and col >= 0:
                if matrix[row][col] == target:
                    return True
                elif matrix[row][col] < target:
                    row += 1
                else:
                    col -= 1
            else:
                return False
# %%
s = Solution()
print(s.searchMatrix(matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3))
print(s.searchMatrix(matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13))

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

打酱油的工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值