leecode第一百四十六题(LRU缓存机制)

class LRUCache {
private:
    unordered_map<int, list<pair<int,int>>::iterator> _m;
    // 新节点或刚访问的节点插入表头,因为表头指针可以通过 begin 很方便的获取到。
    list<pair<int,int>> _list;
    int _cap;
public:
    LRUCache(int capacity) : _cap(capacity) {}

    // O(1)
    // hash 查找,如果找到了,就把 list 中的节点接下来移到头部
    int get(int key) {
        auto it = _m.find(key);
        if (it == _m.end()) return -1;
        int val = it->second->second;
        _list.erase(it->second);
        _list.push_front(make_pair(key, val));
        _m[key] = _list.begin();
        return it->second->second;
    }

    // O(1)
    // 先查找旧 key 是否存在,如果存在,将节点移动到首部。
    // 如果不存在,插入新节点。
    // 如果容量超限,删除最脏的节点。
    void put(int key, int value) {
        auto it = _m.find(key);
        if (it != _m.end()) {
            _list.erase(it->second);
        }

        _list.push_front(make_pair(key, value));
        _m[key] = _list.begin();

        if (_list.size() > _cap) {
            int key = _list.back().first;
            _m.erase(key);
            _list.pop_back();
        }
    }
};

/**
 * Your LRUCache object will be instantiated and called as such:
 * LRUCache* obj = new LRUCache(capacity);
 * int param_1 = obj->get(key);
 * obj->put(key,value);
 */

分析:

写的太丑了,不如人家写的好,只能借鉴别人的。要学的东西好多啊。

原文地址:https://www.cnblogs.com/CJT-blog/p/10711085.html

时间: 2024-08-30 12:19:10

leecode第一百四十六题(LRU缓存机制)的相关文章

leecode第一百四十二题(环形链表II)

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *detectCycle(ListNode *head) { if(head==NULL) return NULL; ListNode *fast_no

【leetcode 简单】 第一百四十六题 最长和谐子序列

和谐数组是指一个数组里元素的最大值和最小值之间的差别正好是1. 现在,给定一个整数数组,你需要在所有可能的子序列中找到最长的和谐子序列的长度. 示例 1: 输入: [1,3,2,2,5,2,3,7] 输出: 5 原因: 最长的和谐数组是:[3,2,2,2,3]. 说明: 输入的数组长度最大不超过20,000. from collections import Counter class Solution: def findLHS(self, nums): """ :type n

一起talk C栗子吧(第一百四十六回:C语言实例--socket属性)

各位看官们,大家好,上一回中咱们说的是socket概述的例子,这一回咱们说的例子是socket属性.闲话休提,言归正转.让我们一起talk C栗子吧! 看官们,我们在上一 回中对socket做了基本的介绍,这一回我们主要是介绍socket的属性. socket主要有三个属性: 域(domain) 类型(type) 协议(protocol) 域表示socket的通信范围.比如,我们通过互联网通信时使用的域为AF_INET(表示IPv4,与现在使用的IPv6对应的域是AF_INET6),我们在本地通

leecode第一百一十四题(二叉树展开为链表)

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void flatten(TreeNode* root) { if(root==NULL)//记住!roo

code第一部分数组:第十六题 数组表示数,加一操作

code第一部分数组:第十六题  数组表示数,加一操作 Given a number represented as an array of digits, plus one to the number. #include <iostream> #include <stdlib.h> #include <stdio.h> #include <vector> using namespace std; int * addone(int *a,int n) { in

经典算法题每日演练——第十六题 Kruskal算法

原文:经典算法题每日演练--第十六题 Kruskal算法 这篇我们看看第二种生成树的Kruskal算法,这个算法的魅力在于我们可以打一下算法和数据结构的组合拳,很有意思的. 一:思想 若存在M={0,1,2,3,4,5}这样6个节点,我们知道Prim算法构建生成树是从”顶点”这个角度来思考的,然后采用“贪心思想” 来一步步扩大化,最后形成整体最优解,而Kruskal算法有点意思,它是站在”边“这个角度在思考的,首先我有两个集合. 1. 顶点集合(vertexs): 比如M集合中的每个元素都可以认

【leetcode 简单】 第九十六题 最长回文串

给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: 假设字符串的长度不会超过 1010. 示例 1: 输入: "abccccdd" 输出: 7 解释: 我们可以构造的最长的回文串是"dccaccd", 它的长度是 7. class Solution(object): def longestPalindrome(self, s): &quo

leecode第二百二十六题(翻转二叉树)

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void core_code(TreeNode* root) { TreeNode* node_temp=

leecode第五十九题(螺旋矩阵 II)

class Solution { public: vector<vector<int>> generateMatrix(int n) { if(n==0)//特殊情况 { vector<vector<int>> empty; return empty; } vector<vector<int>> res; for(int i=0;i<n;i++)//要先初始化 { vector<int> zeros; for(int