码迷,mamicode.com
首页 > 其他好文 > 详细

【leetcode】LRU Cache

时间:2014-08-20 14:04:32      阅读:250      评论:0      收藏:0      [点我收藏+]

标签: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;
            }
        }
    }
};

  

【leetcode】LRU Cache,布布扣,bubuko.com

【leetcode】LRU Cache

标签:des   blog   os   io   for   div   log   new   

原文地址:http://www.cnblogs.com/weixliu/p/3924380.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!