1. 程式人生 > >[LeetCode] 146. LRU Cache java

[LeetCode] 146. LRU Cache java

/**146. LRU Cache
     * @date: 2016年10月27日
     * @description: http://blog.csdn.net/sbitswc/article/details/35899935
*/
private HashMap<Integer, DoubleLinkedListNode> map 
        = new HashMap<Integer, DoubleLinkedListNode>();
    private DoubleLinkedListNode head;
    private DoubleLinkedListNode end;
    private
int capacity; private int len; public LRUCache(int capacity) { this.capacity = capacity; len = 0; } public int get(int key) { if (map.containsKey(key)) { DoubleLinkedListNode latest = map.get(key); removeNode(latest); setHead(latest); return
latest.val; } else { return -1; } } public void removeNode(DoubleLinkedListNode node) { DoubleLinkedListNode cur = node; DoubleLinkedListNode pre = cur.pre; DoubleLinkedListNode post = cur.next; if (pre != null) { pre.next = post; } else
{ head = post; } if (post != null) { post.pre = pre; } else { end = pre; } } public void setHead(DoubleLinkedListNode node) { node.next = head; node.pre = null; if (head != null) { head.pre = node; } head = node; if (end == null) { end = node; } } public void set(int key, int value) { if (map.containsKey(key)) { DoubleLinkedListNode oldNode = map.get(key); oldNode.val = value; removeNode(oldNode); setHead(oldNode); } else { DoubleLinkedListNode newNode = new DoubleLinkedListNode(key, value); if (len < capacity) { setHead(newNode); map.put(key, newNode); len++; } else { map.remove(end.key); end = end.pre; if (end != null) { end.next = null; } setHead(newNode); map.put(key, newNode); } } } } class DoubleLinkedListNode { public int val; public int key; public DoubleLinkedListNode pre; public DoubleLinkedListNode next; public DoubleLinkedListNode(int key, int value) { val = value; this.key = key; }

相關推薦

[LeetCode] 146. LRU Cache java

/**146. LRU Cache * @date: 2016年10月27日 * @description: http://blog.csdn.net/sbitswc/article/details/35899935 */ private H

LeetCode 146 LRU Cache

code hash head width validate return .com opera != Design and implement a data structure for Least Recently Used (LRU) cach

LeetCode 146. LRU Cache

高頻題,需要用unordered_map和list,做到O(1) 需要注意的一點是,list用了splice改變了節點的位置,但是iterator並不會失效,這也代表unordered_map不需要進行更新。(可以把iterator當成指標方便理解) class LRUCache { public

[leetcode] 146. LRU Cache

https://leetcode.com/problems/lru-cache/description/ class LRUCache { private: int _capacity; list<pair<int, int> >

Leetcode 146 LRU Cache(雙向連結串列+STL)

解題思路:用一個雙向連結串列,維護一個最近訪問次序,用map記錄對應key的結點指標。對於get請求,需要將當前結點移動到連結串列的頭位置;對於put操作,如果是更新,則同樣將當前結點移動到頭位置,如果不是更新,則在頭位置插入一個新結點。如果連結串列長度超過快取上限,則刪除末

[Leetcode-146] LRU Cache 最近最少使用頁面置換演算法

題目概要 AC 程式碼 0. 題目概要 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the foll

LeetCode 146. LRU快取機制(java實現)

參考解答 總結:這道題主要要知道選取何種資料結構並且在 O(1) 時間複雜度內完成這兩種操作? O(1) 的get方法肯定要用到HashMap() LinkedList(雙向連結串列)可以以O(1)時間複雜度,很方便地實現資料的插入和刪除 所以,將兩個資料結構聯合使用,Ha

[leetCode] LRU Cache (Java)

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

146. LRU Cache

logs pac ++ pri capacity private r+ csharp sha class LRUCache { class DNode{ public int val; public int key;

[LeetCode]146.LRU緩存機制

problem span 密鑰 單向 arraylist per pri integer .com 設計和實現一個 LRU(最近最少使用)緩存 數據結構,使它應該支持以下操作: get 和 put 。 get(key) - 如果密鑰存在於緩存中,則獲取密鑰的值(總是正數)

[leetcode]146. LRU CacheLRU快取

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

146. LRU Cache - Hard

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

[Leetcode]146.LRU快取機制

Leetcode難題,題目為: 運用你所掌握的資料結構,設計和實現一個  LRU (最近最少使用) 快取機制。它應該支援以下操作: 獲取資料 get 和 寫入資料 put 。 獲取資料 get(key) - 如果金鑰 (key

LeetCodeLRU Cache 解題報告

題外話:才連續寫了幾篇部落格,部落格排名竟然就不再是“千里之外”了,已經進入2萬名之內了。再接再厲,加油! Design and implement a data structure for Least Recently Used (LRU) cache. It sho

leetcode146.(Hard)LRU Cache

解題思路: 用map來記錄資料 用list陣列更新資料的使用情況 提交程式碼: class LRUCache { Map<Integer,Integer> map=new HashMap<>(); List<Integer> histor

LRU Cache leetcode java

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

leetcode LRU Cache Golang

Golan last n) ise port bool lan empty println package main import( "fmt" ) type Node struct { Key string Val string Pre *Node Nex

LeetCode ---LRU Cache

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

Leetcode 146LRU快取機制(超詳細的解法!!!)

運用你所掌握的資料結構,設計和實現一個 LRU (最近最少使用) 快取機制。它應該支援以下操作: 獲取資料 get 和 寫入資料 put 。 獲取資料 get(key) - 如果金鑰 (key) 存在於快取中,則獲取金鑰的值(總是正數),否則返回 -1。 寫入資料 put(key,

LeetCode 146 Hard 實現LRU演算法 2種解法 Python

""" My Method 演算法:雙向連結串列+字典 思路: 首先一定要回顧一下LRU演算法,其實就是一個優先佇列,最近使用過的在隊尾,時間久的在隊頭,該佇列 是有長度限制的,超出長度後最久未訪問的元素出佇列,類似於一種先進先出。然後訪問元素的時候,