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
- LRU leetcode 代码实现
- leetcode之LRU实现
- LeetCode LRU Cache (Java实现)
- LRU Cache算法实现(leetcode)
- 【leetcode】LRU
- LeetCode LRU 缓存 LRU Cache
- [LeetCode]146. LRU Cache 深入浅出讲解和代码示例
- 【Leetcode】:LRU Cache_缓存淘汰算法LRU的设计与实现
- Leetcode——146. LRU Cache 一个优雅的LRU的C++实现
- LeetCode 146 LRU Cache(list+unordered_map实现LRU缓存算法)
- [Leetcode] LRU Cache. 哈希表+双向链表之实现
- 页面置换算法LRU实现--leetcode O(1)时间复杂度
- LRU页面置换算法原理及Java代码的实现
- 给大家推荐一个LRU实现算法的java 代码
- 缓存淘汰算法--LRU算法(java代码实现)
- 缓存淘汰算法--LRU算法(java代码实现)
- LRU Cache | leetcode
- Leetcode: LRU Cache
- Android开发学习笔记:Service的简介和启动方式
- Java8揭秘(二)Java 8中的 Lambda表达式
- Android开发学习笔记:RadioButton和CheckBox浅析
- 搭hibernate框架时遇到的几个问题
- Servlet中Web.xml的配置详解
- LRU leetcode 代码实现
- 【Cocos2d-x 016】 骨骼动画
- Java8揭秘(三)Default 方法
- location.href的用法
- Edit Distance -- LeetCode
- Java8揭秘(四)Java集合类库的批量数据操作
- strcpy 和 strncpy的区别和实现
- JavaSE 线程通信 wait() notify()
- ORACLE 绑定变量用法总结