"Leetcode" LRU Cache Resolution Report

Source: Internet
Author: User

Interposed: Only a few consecutive blogs have been written, and the blog rank is no longer actually "far away" that. We have entered within 210,000.

Continue. Oil!

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 ('ll 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 is reached its capacity, it should invalidate the least recently used item before inserting a new item.

Test instructions

Design and implement a cache that supports get and set operations:

Get (key)-returns its value when it exists. otherwise returns-1.

Set (key)-Inserts a new value when it does not exist. Updates its value when it exists. Note When the volume is full, you need to delete the key that has not been visited for the longest time. Delete it. and insert a new key.


==================== Map+list Realization Method ====================

Ideas

The map structure is used to realize <key, value> storage and reading.

Use a list to record key access time. Recent interviews have been placed at the end of the list. The first key in the list indicates the longest time you haven't been interviewed.

"Java Code"

Class LRUCache {    hashmap<integer, integer> map;    arraylist<integer> list;    int capacity;        public LRUCache (int capacity) {        map = new Hashmap<integer, integer> (capacity);        List = new arraylist<integer> (capacity);        This.capacity = capacity;    }        public int get (int key) {        if (map.get (key) = = null) return-1;        List.remove (The new Integer (key));         List.add (key);        return Map.get (key);    }        public void set (int key, int value) {    if (map.get (key) = null) {//original exists key    Map.put (key, value);    List.remove (The new Integer (key));             List.add (key);    } else {//The original does not exist key    if (Map.size () < capacity) {//capacity is dissatisfied    with Map.put (key, value);    List.add (key);    } else {//capacity full    int leastkey = list.remove (0);                List.add (key);                Map.Remove (Leastkey);                Map.put (key, value);}}}    

"Attention Point"

The title requirement is least recently used, not only set when you want to update the list,get when you also update the list.

When set. It is necessary to infer that the map has no such value, and if you do not infer whether the map is full or not, assume that the map is full, and then infer that there is no value in the map. That's the wrong thing to do.

This value is assumed when the map is full. The direct update is good, but first to infer whether the map is full. will result in the deletion of values that have not been interviewed for the longest time.


"General Solution"

Pass often uses the elements of the doubly linked list to record the longest time not being accessed, since the doubly linked list can be O (1) to move the node within a certain period of time, deleting the head and tail nodes.

In the above code list implementation, remove it when actually traversing the entire list in order to find a node.

Leetcode There is no time for requests, the interview will certainly ask.


"Leetcode" LRU Cache Resolution Report

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.