国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

LRU Cache -- Lintcode 134

2019-11-14 11:59:14
字體:
供稿:網(wǎng)友

原題連接: http://www.lintcode.com/en/PRoblem/lru-cache/

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.

題目要求設(shè)計和實現(xiàn)一個給LRU Cache用的數(shù)據(jù)結(jié)構(gòu),要求包含兩個操作, get 和 set。首先題目有一點沒有講清楚,那就是當(dāng)要插入的(key,value)已經(jīng)存在時的行為。題目只是說當(dāng)key不存在時插入,而當(dāng)要插入的key已經(jīng)存在時的行為如何,題目并沒有說明,是覆蓋舊的value還是直接返回。我是通過提交代碼,發(fā)現(xiàn)當(dāng)要插入的key已經(jīng)存在時,用新的value覆蓋舊的value。網(wǎng)絡(luò)上有很多關(guān)于用一個hash table 和一個list實現(xiàn)的代碼,我就不再累述。下面講述我用兩個hash table的實現(xiàn)。一個叫cache的用于保存數(shù)據(jù)的<key,value>,另一個叫stamp用于保存<key,timeStamp>。get操作很簡單,直接在cache里面查找并返回相應(yīng)的結(jié)果,同時要更新stamp里的時間戳。set操作先檢查要插入的key是否已經(jīng)存在。如果存在,更新value和time stamp并返回。如果不存在,并且cache的數(shù)據(jù)個數(shù)小于capacity,直接執(zhí)行插入操作。如果capacity已經(jīng)滿了,就從stamp里查找時間戳最小的key,此時需要O(capacity)的時間復(fù)雜度。然后把對應(yīng)的數(shù)據(jù)刪除并插入當(dāng)前的<key,value>。 C++實現(xiàn)如下:

class LRUCache{public:    // @param capacity, an integer    int currTime;    int cap;    unordered_map<int, int> cache;    unordered_map<int, int> stamp;        LRUCache(int capacity) {        // write your code here        currTime = 0;        cap = capacity;        cache.reserve(capacity);        stamp.reserve(capacity);    }        // @return an integer    int get(int key) {        // write your code here        if (cache.count(key))        {            ++currTime;            stamp[key] = currTime;                        return cache[key];        }                return -1;    }    // @param key, an integer    // @param value, an integer    // @return nothing    void set(int key, int value) {        // write your code here            ++currTime;                 if (cache.count(key))        {            cache[key] = value;            stamp[key] = currTime;            return;        }        if (cache.size() < cap)        {            cache[key] = value;            stamp[key] = currTime;                        return;        }                int minTime = INT_MAX;        unordered_map<int, int>::iterator leIt, it = stamp.begin();                while (it != stamp.end())        {            if (it->second < minTime)            {                minTime = it->second;                leIt = it;            }                        ++it;        }                unordered_map<int, int>::iterator cacheIt = cache.find(leIt->first);        cache.erase(cacheIt);        cache[key] = value;                stamp.erase(leIt);        stamp[key] = currTime;    }};


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 尤溪县| 仙桃市| 井冈山市| 拜泉县| 赣榆县| 云霄县| 和龙市| 临澧县| 鄂尔多斯市| 太谷县| 青州市| 新野县| 江达县| 图木舒克市| 青浦区| 巴塘县| 莫力| 乐山市| 河曲县| 北辰区| 上饶市| 达日县| 阿拉尔市| 随州市| 天长市| 逊克县| 永寿县| 疏附县| 丹凤县| 宜城市| 桃源县| 宜黄县| 顺义区| 盐亭县| 永吉县| 勐海县| 工布江达县| 上杭县| 交口县| 和龙市| 托里县|