LRU leetcode 代码实现

来源:互联网 发布:量化交易之路 python 编辑:程序博客网 时间:2024/05/16 08:32



最近在刷leetcode上面的题目,尝试做了一下LRU的题目,我自己明明能跑得通的程序放在leetcode上面测试会出现Runtime time Error, 而且测试样例也不大,完全不可能超时呀!!!肿么会有这种情况

再加上我借鉴了: http://blog.csdn.net/hexinuaa/article/details/6630384 上面的方法,按理说时间复杂度已经降到低


那位大神能帮我看一下代码,还是我忘记了了leetcode的一些规范:


#include <iostream>using namespace std;struct biNode{    biNode *next;    biNode *prev;    struct lkNode* p_lkNode;    int value;    biNode(){next=NULL;prev=NULL;p_lkNode=NULL;value=0;}};struct lkNode{    lkNode *next;    biNode *p_biNode;    int data;    lkNode(){next=NULL;p_biNode=NULL;data=0;}};class LRUCache{private:    int used,cap;    struct lkNode** hash_kNode;    struct biNode* p_hdNode;    inline int hash_fn(int key,int capacity){return (key%capacity);}public:    LRUCache(int capacity) {        cap = capacity;        hash_kNode = new lkNode*[capacity];  // check if every member point to NULL        // try it, if not initial to NULL, will it be tested with memory leakage.        for (int i=0;i<capacity;i++){            hash_kNode[i] = NULL;        }        p_hdNode = new struct biNode;        p_hdNode->next = NULL;        p_hdNode->prev = NULL;        used = 0;    }    int get(int key) {        int ret;        int h_key = hash_fn(key,cap);        if (hash_kNode[h_key] == NULL) return -1;        else{            //cout<<hash_kNode[h_key]->data;            lkNode* tmp_scan = hash_kNode[h_key];            while(tmp_scan!=NULL){                if (tmp_scan->data != key){                    tmp_scan = tmp_scan->next;                    continue;                }                else{                    biNode* tmp_mthbiNode = tmp_scan->p_biNode;                    ret = tmp_mthbiNode ->value;                    biNode* tmp_hdnext = p_hdNode->next;                    biNode* tmp_hdprev = p_hdNode->prev;                    biNode* tmp_fdnext = tmp_mthbiNode->next;                    biNode* tmp_fdprev = tmp_mthbiNode->prev;                    p_hdNode->next = tmp_mthbiNode;                    tmp_mthbiNode->prev = tmp_hdprev;                    tmp_mthbiNode->next = tmp_hdnext;                    tmp_hdprev->next = tmp_mthbiNode;                    tmp_hdnext->prev = tmp_mthbiNode;                    tmp_fdprev->next = tmp_fdnext;                    tmp_fdnext->prev = tmp_fdprev;                    return ret;                }            }        }        return -1;    }    void set(int key, int value) {        int h_key = hash_fn(key,cap);        lkNode* tmp_scan = hash_kNode[h_key];        lkNode* tmp_scanBak=NULL;        while(tmp_scan!=NULL){            tmp_scanBak = tmp_scan;            tmp_scan = tmp_scan->next;        }        // create new lkNode(and init) and corresponding biNode        lkNode* new_lkNode= new struct lkNode;        if (tmp_scanBak != NULL)        tmp_scanBak->next = new_lkNode;        else{            hash_kNode[h_key] = new_lkNode;        }        biNode* new_biNode = new struct biNode;        new_lkNode->p_biNode = new_biNode;        new_lkNode->next = NULL;        new_lkNode->data = key;        new_biNode->value = value;        new_biNode->p_lkNode = new_lkNode;        new_biNode->next = new_biNode; // in order to deal with first insert scenario        new_biNode->prev = new_biNode;        if (used == cap){            //first delete tail biNode (find lkNode first, otherwise it cannot be pointed!)            biNode* del_biNode = p_hdNode->prev;            lkNode* del_lkNode = del_biNode->p_lkNode;            int del_key = del_lkNode->data;            int h_delKey = hash_fn(del_key,cap);            biNode* tmp_tlprev = del_biNode->prev;            biNode* tmp_tlnext = del_biNode->next;            p_hdNode->prev = tmp_tlprev;            tmp_tlprev->next = tmp_tlnext;            tmp_tlnext->prev = tmp_tlprev;            delete del_biNode;            del_biNode = NULL;            //then delete corresponding lkNode            tmp_scan = hash_kNode[h_delKey];            tmp_scanBak = tmp_scan;            int flag = 0;            while(tmp_scan->data!=del_key){                flag = 1;                tmp_scanBak = tmp_scan;                tmp_scan = tmp_scan->next;            }            if (flag == 1){                flag = 0;                tmp_scanBak->next = tmp_scan->next;            }            else            hash_kNode[h_delKey] = tmp_scan->next;            delete tmp_scan;            tmp_scan = NULL;            used--;        }        //insert new biNode to head (and link lk_Node)        if (p_hdNode->next == NULL && p_hdNode->prev == NULL){            p_hdNode->next = new_biNode;            p_hdNode->prev = new_biNode;        }        else{            biNode* tmp_hdnext = p_hdNode->next;            biNode* tmp_hdprev = p_hdNode->prev;            p_hdNode->next = new_biNode;            new_biNode->prev = tmp_hdprev;            new_biNode->next = tmp_hdnext;            tmp_hdnext->prev = new_biNode;            tmp_hdprev->next = new_biNode;        }        //cout<<hash_kNode[h_key]->data;        // used one point;        used ++;    }};int main(){    LRUCache lru(1);    lru.set(2,1);    cout<<lru.get(2);    lru.set(3,2);    cout<<lru.get(2);    cout<<lru.get(3);    return 0;}

不剩感激,我的输出是1,-1,2,应该没错吧!

难道我理解的LRU有错???疑问

0 0
原创粉丝点击