【leetcode】146. LRU Cache【H】【9】

【信息科学与工程学】【数据科学】数据科学领域 第六篇 算法设计 07 堆、栈、表、矩阵、集合、队列、数组、字符串、树、图、动态规划、结构体、元组的算法01 当前段必须包含所有字符的最后出现位置。1. 字符串分割成最多片段2. 基因序列功能域划分3. 文本段落划分4. 音频声道分离5. 图像色彩分区6. 社交网络社群划分7. 蛋白质结构域8. 音乐乐章分段9. 代码模块划分10. 章节划分特征:一次扫描,贪心确定边界。 阅读详情



Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.


Subscribe to see which companies asked this question


用字典来记录cache的内容

用一个list,模拟队列,来记录使用的顺序

python的 collections是个好动西


class LRUCache(object):

    def __init__(self, capacity):
        self.l = capacity
        self.q = []
        self.dic = {}
        """
        :type capacity: int
        """
        

    def get(self, key):
        if key not in self.q:
            return -1
        else:
            #print key,self.q
            self.q.remove(key)
            res = self.dic.get(key,-1)
        
        self.q += key,
        if len(self.q) > self.l:
            self.q.pop(0)
        return res
        """
        :rtype: int
        """
        

    def set(self, key, value):
        if key in self.q:
            self.q.remove(key)
        self.q += key,
        
        if len(self.q) > self.l:
            self.q.pop(0)
        self.dic[key] = value
        #print self.q,self.dic
        """
        :type key: int
        :type value: int
        :rtype: nothing
        """


高能物理相关软件 国内高能物理实验背景     研究物质最深层次结构及相互作用的前沿基础科学。 现代高能物理实验的要素    实例源 、 探测器、   电子学、  触发 DAQ、  原始数据、 刻度、重建 、物理分析、  成果  。 离线软件的地位和作用。    是硬件设备和物理学家的桥梁,是从数据到物理成果转化中的关键和重要的环节。 高能物理实验及其特点 独一无二性,实验规模越来越大,努力省钱(使用 阅读详情

相关推荐

告别F5失灵!手把手教你为Cursor配置.Net Core控制台项目专属调试环境

本文详细指导如何在Cursor编辑器中为.Net Core控制台项目配置专属调试环境,解决F5失灵等常见问题。从调试器选择到launch.json配置,再到高级调试技巧,帮助开发者高效搭建完整的调试工具链,提升开发效率。

weixin_28834765的博客 286

哈希表原理与LeetCode高频题型解析

哈希表作为基础数据结构,通过哈希函数实现O(1)时间复杂度的快速查找,是算法面试中的核心考点。其核心原理是将键映射到存储位置,利用空间换时间提升查询效率。在工程实践中,哈希表广泛应用于缓存系统、数据库索引等场景,并通过链地址法或开放寻址法处理冲突。LeetCode高频题型如两数之和、字母异位词分组等,都依赖哈希表优化时间复杂度。掌握负载因子、动态扩容等机制,能有效提升系统设计能力。对于海量数据处理,布隆过滤器等衍生结构提供了更优的空间效率解决方案。

weixin_34050005的博客 399

华为OD机试_2025 B卷_模拟消息队列(Python,100分)(附详细解题思路)

时间排序:确保事件处理顺序正确消费者管理:集合操作高效维护状态优先级实现:利用消费者ID自然排序边界处理:正确处理空消费者情况。

测试工程师的成长之路 1120

前端codetop手撕算法

核心思路:最优解是哈希表法,用 O (n) 时间复杂度解决,空间复杂度 O (n)(存储哈希表);关键逻辑:遍历过程中计算补数,查哈希表是否存在,存在则返回,不存在则存入;优势对比:哈希表法比暴力法效率提升百倍以上(尤其数组长度大时),符合进阶要求。46. 全排列方法时间复杂度空间复杂度特点迭代法O(n)O(1)空间更优,面试推荐递归法O(n)O(n)逻辑更简洁,但有递归栈开销核心逻辑迭代法:双指针遍历,逐个反转节点指向;递归法:先反转子链表,再调整当前节点指向。

2302_80168280的博客 574

华为CCA架构软件栈VehicleStack解析:3层服务化设计与AOS/VOS双核OS

本文深入解析华为CCA架构下的VehicleStack软件平台,重点介绍其三层服务化设计(硬件抽象层、服务框架层、应用层)与AOS/VOS双核操作系统协同机制。该架构通过计算与通信的深度融合,推动汽车电子电气架构向集中式发展,为智能汽车提供灵活、高效的软件定义汽车解决方案。

weixin_33491377的博客 324

资源描述框架RDF及Turtle

文章目录1. RDF1.1 简介1.2 格式1.3 RDF的要求2. Turtle2.1 语法2.1.1 简单三元组2.1.2 Turtle中的命名空间2.1.3 Base URI2.1.4 谓语列表2.1.5 宾语列表2.1.6 空白节点(bNodes)2.1.7 数据类型2.1.8 Class membership2.1.9 空URI2.1.10 集合 1. RDF 1.1 简介 RDF(Resource Description Framework)语义网的标准数据模型,是一个用于描述 Web 上的资

doublewhite233的博客 2494

【leetcode】447. Number of Boomerangs【E】

Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of points (i, j, k) such that the distance between iand j equals the distance between i and k (the order of t

sscssz的博客 2462

深入解析Apollo Lattice Planner:自动驾驶轨迹规划的核心算法与实践

本文深入解析了百度Apollo自动驾驶平台中的核心轨迹规划算法——Lattice Planner。文章详细阐述了其作为车辆“即时决策者”的工作原理,通过“采样-评估”的六步流程(Frenet坐标转换、状态采样、轨迹生成、硬约束过滤、代价函数打分及决策平滑),在复杂路况下实时规划出安全、舒适、高效的行驶轨迹。该算法是自动驾驶实现智能决策与精准控制的关键技术。

weixin_29090677的博客 830

【leetcode】378. Kth Smallest Element in a Sorted Matrix【M】【88ms】

Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that it is the kth smallest element in the sorted order, not t

sscssz的博客 1865

一文快速教你如何下载国自然模版

5. 成功进入“相关文件查看”页面后,点击左侧的“项目申请文件“,如果你此次申请的是“面上项目”,选择对应的下拉框,找到对应的模版下载即可。6. 🫑们发现,在当前页面点击“青年科学基金项目”,左侧出现的是“原国家杰出青年科学基金项目”。7. 进入下一页后,点击“青年科学基金项目”,便可以在右侧找到🫑们要找的模版啦。2. 成功登陆后,点击1处的“首页”,在首页界面,再点击2处的“项目批准”。8. 点击上图3处的“文件详情”,就可以找到可以下载的模版啦(如下图)。3. 从“项目批准”进入后,点击“管理”。

薰珞婷紫小亭子的博客 2399

Android 15.0 首次开机默认授予app运行时权限(去掉运行时授权弹窗)

在15.0的系统rom产品定制化开发中,在6.0以后对于权限的申请,都需要动态申请,所以会在系统首次启动后,在app的首次运行时, 会弹出授权窗口,会让用户手动授予app运行时权限,在由于系统产品开发需要要求默认授予app运行时权限,不需要用户默认授予运行时弹窗,所以需要在首次开机默认授予所有app运行时权限

安卓兼职framework和app工程师的博客 1204

【leetcode】398. Random Pick Index【M】

Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array. Note: The array size c

sscssz的博客 1398

YOLOv5源码逐行超详细注释与解读(5)——配置文件yolov5s.yaml

全网最详细的YOLOv5源码解读之配置文件yaml。逐行注释,逐句讲解,一文带你了解yaml。小白必看!

路人贾的博客 1万+

【leetcode】438. Find All Anagrams in a String【E】

Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be lar

sscssz的博客 1391

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

【LeetCode】515. Find Largest Value in Each Tree Row【E】【87】

You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 9 Output: [1, 3, 9] Subsc

sscssz的博客 1367

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

203. Remove Linked List Elements【E】【70】【leetcode】

Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6,  val = 6 Return: 1 --> 2 --> 3 --> 4 --> 5 Credits: Special thank

sscssz的博客 751

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

341. Flatten Nested List Iterator【M】【42】

Given a nested list of integers, implement an iterator to flatten it. Each element is either an integer, or a list -- whose elements may also be integers or other lists. Example 1: Given the list [

sscssz的博客 687

【LeetCode】508. Most Frequent Subtree Sum【M】【40】

Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (includi

sscssz的博客 620

105. Construct Binary Tree from Preorder and Ignorer Traversal【M】【35】【再来一遍】

Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. Subscribe to see which companies asked this q

sscssz的博客 610

1. Two Sum【E】【59+25】【leetcode】

Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution. Example: Given nums =

sscssz的博客 584

【LeetCode】451. Sort Characters By Frequency【M】【68】

Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: "tree" Output: "eert" Explanation: 'e' appears twice while 'r' and 't' both appear once. So 'e

sscssz的博客 528

347. Top K Frequent Elements【M】【VIP】【字典排序】

Given a non-empty array of integers, return the k most frequent elements. For example, Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note:  You may assume k is always valid, 1 ≤ k ≤ number

sscssz的博客 519

103. Binary Tree Zigzag Level Order Traversal【M】

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary

sscssz的博客 485

49. Group Anagrams 【M】【47】

Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"],  Return: [   ["ate", "eat","tea"],   ["nat","tan"],   ["bat"] ] Note:

sscssz的博客 437

LeetCode2461~2480题目解析

LeetCode2461~2480题目解析

jiaomubai的博客 137

LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等)

LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等) 题目描述: 给定三个字符串 s1、s2、s3,请你帮忙验证 s3 是否是由 s1 和 s2 交错 组成的。 两个字符串 s 和 t 交错 的定义与过程如下,其中每个字符串都会被分割成若干 非空 子字符串: - s = s1 + s2 + ... + sn - t = t1 + t2 + ... + tm - |n - m| <= 1 - 交错 是 s1 + t

huayimenghan的博客 210
上一篇: 106. Construct Binary Tree from Inorder and Postorder Traversal【M】【31】
下一篇: mysql 远程连接其他主机上的数据库 + 10038错误
Vicky_Vicky_990121
博客等级 码龄14年 23粉丝 187原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值