【leetcode】LRU Cache

來源:互聯網
上載者:User

標籤:des   blog   os   io   for   div   log   new   

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.

 

1 st ( 15tries)

class LRUCache{public:int capacity;int length;struct Node {int key;int value;Node *next;Node *pre;};Node *head;Node *tail;class HashTable {public:unordered_map<int,Node*> innertable;Node *get(int key) {if( innertable.count(key) )return innertable[key];return NULL;}bool del(int key) {unordered_map<int,Node*>::iterator iter;iter = innertable.find(key);if(iter != innertable.end()) {innertable.erase(iter);return true;}return false;}bool add(int key,Node *node) {if( !innertable.count(key) ) {innertable[key] = node;return true;}return false;}};HashTable hashtable;LRUCache(int capacity) {this->capacity = capacity;length = 0;head = new Node();tail = new Node();head->next = tail;head->pre = NULL;tail->next = NULL;tail->pre = head;}int get(int key) {Node *tmp = hashtable.get(key);if(tmp == NULL)return -1;else {tmp->pre->next = tmp->next;tmp->next->pre = tmp->pre;tmp->next = head->next;head->next->pre = tmp;head->next = tmp;tmp->pre = head;return tmp->value;}}void set(int key, int value) {Node *tmp = hashtable.get(key);if(tmp != NULL) {tmp->pre->next = tmp->next;tmp->next->pre = tmp->pre;tmp->next = head->next;head->next->pre = tmp;head->next = tmp;tmp->pre = head;tmp->value = value;}else {if( length < capacity ) {Node *newNode = new Node();newNode->key = key;newNode->value = value;newNode->next = head->next;head->next->pre = newNode;head->next = newNode;newNode->pre = head;length++;hashtable.add(key,newNode);}else {Node *newNode = new Node();newNode->key = key;newNode->value = value;newNode->next = head->next;head->next->pre = newNode;head->next = newNode;newNode->pre = head;Node *t = tail->pre;int delkey = t->key;t->pre->next = tail;tail->pre = t->pre;delete t;hashtable.del(delkey);hashtable.add(key,newNode);}}}};

  

2nd ( 11 tries)

class LRUCache{public:    struct ListNode {        int key;        int value;        ListNode *pre;        ListNode *next;        ListNode(int k,int v) {            key = k;            value = v;        }    };    //LRU hashtable!!!    //key->ListNode!!!    //double link list!!!    ListNode *head;    ListNode *tail;    int cap;    int size;    unordered_map<int,ListNode*> hashtable;    LRUCache(int capacity) {        cap = capacity;        size = 0;        head = new ListNode(0,0);        tail = new ListNode(0,0);        head->next = tail;        head->pre = NULL;        tail->pre = head;        tail->next = NULL;    }        int get(int key) {        unordered_map<int,ListNode*>::iterator iter;        iter = hashtable.find(key);        if( iter != hashtable.end() ) {            ListNode *updateL = iter->second;            updateL->pre->next = updateL->next;            updateL->next->pre = updateL->pre;            //head            updateL->next = head->next;            head->next->pre = updateL;            head->next = updateL;            updateL->pre = head;            return iter->second->value;        }        else {            return -1;        }    }        void set(int key, int value) {        unordered_map<int,ListNode*>::iterator iter;        iter = hashtable.find(key);        //exist???        if( iter != hashtable.end() ) {            //change value,goto head            ListNode *findL = iter->second;            findL->value = value;            //delete            findL->pre->next = findL->next;            findL->next->pre = findL->pre;            //head            findL->next = head->next;            head->next->pre = findL;            head->next = findL;            findL->pre = head;        }        else {            if( size == cap ) {                //delete last one                ListNode *delL = tail->pre;                hashtable.erase( hashtable.find(delL->key) );                delL->pre->next = delL->next;                delL->next->pre = delL->pre;                delete delL;                //create ListNode                ListNode *addL = new ListNode(key,value);                hashtable[key] = addL;                addL->next = head->next;                head->next->pre = addL;                head->next = addL;                addL->pre = head;            }            else {                ListNode *addL = new ListNode(key,value);                hashtable[key] = addL;                size++;                addL->next = head->next;                head->next->pre = addL;                head->next = addL;                addL->pre = head;            }        }    }};

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.