【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) - 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.

import java.util.HashMap;
import java.util.LinkedList;

class ListNode{
	int key, value;
	ListNode next, prev;
	public ListNode()
	{
		this.key = this.value = 0;
		next = prev = null;
	}
	public ListNode(int key, int val)
	{
		this.key = key;
		this.value = val;
		next = prev = null;
	}
}

public class LRUCache {
	private ListNode head,tail;
	private HashMap<Integer, ListNode> hashmap;
	private int size, capacity;

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	public LRUCache(int capacity) {
        this.size = 0;
        this.capacity = capacity;
        head = new ListNode();
        tail = new ListNode();
        head.next = tail;
        tail.prev = head;
        hashmap = new HashMap<Integer, ListNode>();
    }

    public int get(int key) {
        if (hashmap.containsKey(key)) {
			ListNode cur = hashmap.get(key);
			//detach the node in the list
			cur.prev.next = cur.next;
			cur.next.prev = cur.prev;
			//set the visting node be the firstNode.
			ListNode tmp = head.next;
			head.next = cur;
			cur.prev = head;
			cur.next = tmp;
			tmp.prev = cur;
			return cur.value;
		}
        else return -1;
    }

    public void set(int key, int value) {
        if (hashmap.containsKey(key)) {
			ListNode cur = hashmap.get(key);
			cur.value = value;
			//detach
			cur.prev.next = cur.next;
			cur.next.prev = cur.prev;
			//set the visting node be the firstNode.
			ListNode tmp = head.next;
			head.next = cur;
			cur.prev = head;
			cur.next = tmp;
			tmp.prev = cur;
		}
        else {
			if (size == capacity) {
				ListNode last = tail.prev;
				hashmap.remove(last.key);
				tail = last;
				last = null;
				size--;
			}
			ListNode cur = new ListNode(key, value);
			hashmap.put(key, cur);
			//set the new node be the firstNode.
			ListNode tmp = head.next;
			head.next = cur;
			cur.prev = head;
			cur.next = tmp;
			tmp.prev = cur;
			size++;
		}
    }

}

【LeetCode】LRU Cache

时间: 2024-10-23 14:26:22

【LeetCode】LRU Cache的相关文章

【LeetCode】LRU Cache 解决报告

插话:只写了几个连续的博客,博客排名不再是实际"远在千里之外"该.我们已经进入2一万内. 再接再厉.油! 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 ke

【leetcode】LRU Cache(hard)★

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(

【Lintcode】LRU Cache, Data Stream Median

主要是priority_queue的用法 一个是内置类型优先队列怎么设置小根堆(默认大根堆) 如果是自定义数据结构,有两种办法 1.定义这种数据结构的比较符号,就可以当成内置类型整 2.传进去一个重载()的类,当小于号用,默认还是大根堆,也许传进去的是个callable object都行的吧,我试了一下函数好像不行,不懂,不管了 LRU Cache class LRUCache{ public: // @param capacity, an integer int Time; typedef i

【LeetCode】设计题 design(共38题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure design [173]Binary Search Tree Iterator [208]Implement Trie (Prefix Tree) [211]Add and Search Word - Data structure desig

【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) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

【LeetCode】Pascal&#39;s Triangle

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 这题别想用通项公式做,n choose m里面的连乘必然溢出,老老实实逐层用定义做. class Solution { public: vector<vector<