↓ 跳过正文
  1. Posts/

leetcode 146. LRU Cache(list+unordered_map)

·457 字·1 分钟

请实现最近最少使用缓存(Least Recently Used (LRU) cache)类,需要支持 get, set,操作。 get 操作,给出 key,获取到相应的 value (value 为非负数),如果不存在返回-1, 如果存在此 key 算作被访问过。 set 操作,设置 key,如果 key 存在则覆盖之前的 value (此时相当于访问过一次)。 如果 key 不存在,需要进行插入操作,如果此时已经 key 的数量已经到达 capacity, 这样需要淘汰掉最近最少使用(也就是上次被使用的时间距离现在最久的)的那 一项。

要求get和set的时间复杂度都是O(1)

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2017年08月18日 星期五 00时00分22秒
 4File Name :LRU.cpp
 5 ************************************************ */
 6
 7class LRUCache{
 8    private:
 9    //map:<key,Value>
10    //Value:pair<value,time>
11    //time:vector? list?
12    typedef  unordered_map<int, pair<int , list<int>::iterator > >Cache;
13    Cache cache;
14    list<int>hit_seq; //头部最新元素,尾部最旧元素
15    int siz;
16
17        #define fst first
18        #define sec  second
19        #define MP make_pair
20    void hit(Cache::iterator it) //access once
21    {
22        int key = it->fst;
23        hit_seq.erase(it->sec.sec);
24        hit_seq.push_front(key); //更新访问序列
25        it->sec.sec = hit_seq.begin(); //更新该key的最后访问时间
26
27    }
28    public:
29    LRUCache(int capacity)
30    {
31        siz = capacity;
32    }
33    int get(int key)
34    {
35        auto it = cache.find(key);
36        if (it==cache.end()) return -1;
37        hit(it);
38        return it->sec.fst;
39    }
40    void put(int key, int value)
41    {
42        auto it = cache.find(key);
43        if (it != cache.end()) hit(it);
44        else
45        {
46        if (siz == cache.size())
47        {
48            cache.erase(hit_seq.back());
49            //淘汰hit序列最后的,也就是最旧的
50            hit_seq.pop_back();
51        }
52        hit_seq.push_front(key);
53        }
54        cache[key]=MP(value,hit_seq.begin());
55
56    }
57};

相关文章

leetcode162. Find Peak Element (O(lgn)复杂度寻找峰值)

·448 字·1 分钟
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fine. You may imagine that num[-1] = num[n] = -∞. For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.

leetcode 152. Maximum Product Subarray (最大连续子序列乘积,dp)

·289 字·1 分钟
Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 思路:由于有正,有负,还有0.。。所以比最大子串之和要复杂一些。。。 dp[i].max表示到当前位置的最大乘积。

leetcode 228. Summary Ranges

·412 字·1 分钟
Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. 题意:把连续的数连续表示 思路:模拟。注意有负数,注意有-2147483648这种数据。 本来还想着,可能是leetcode加数据的审核机制太松,导致被人加了奇怪的数据。。。

leetcode 209. Minimum Size Subarray Sum (尺取法)

·248 字·1 分钟
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn’t one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7, the subarray [4,3] has the minimal length under the problem constraint 思路:尺取即可。。好久没写,竟然调了半天。。。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2017年04月13日 星期四 20时48分00秒 4File Name :209.cpp 5************************************************ */ 6class Solution { 7 8public: 9 10 int ruler(vector<int>nums,int tar,int n) 11 { 12 int head = 0; 13 int tail = 0; 14 int sum = 0 ; 15 int res = 0x3f3f3f3f; 16 while (tail<n&&head<=tail) 17 { 18 sum = sum + nums[tail]; 19 if (sum>=tar) 20 { 21 res = min(res,tail-head+1); 22 while (sum>=tar&&head<tail) 23 { 24 sum-=nums[head]; 25 head++; 26 } 27 if (sum>=tar) 28 { 29 res = min(res,tail-head+1); 30 } 31 else 32 { 33 head--; 34 sum+=nums[head]; 35 res = min(res,tail-head+1); 36 } 37 } 38 39 40 41 tail++; 42 } 43 return res==0x3f3f3f3f?0:res; 44 } 45 46 47 int minSubArrayLen(int s, vector<int>& nums) { 48 int n = nums.size(); 49 int res = ruler(nums,s,n); 50 return res; 51 52 53 } 54 55};