-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path146. LRU缓存机制
More file actions
60 lines (47 loc) · 1.44 KB
/
146. LRU缓存机制
File metadata and controls
60 lines (47 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class LRUCache:
def __init__(self, capacity: int):
import collections
self.dic = collections.OrderedDict()
self.capacity=capacity
def get(self, key: int) -> int:
a=self.dic.get(key,-1)
if a!=-1:
self.dic.move_to_end(key)
return a
else:
return -1
def put(self, key: int, value: int) -> None:
self.dic[key]=value
self.dic.move_to_end(key)
if len(self.dic)>self.capacity:
self.dic.popitem(last=False)
## 使用有序字典完成
# Your LRUCache object will be instantiated and called as such:
# obj = LRUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)
# 普通的暴力法
class LRUCache:
def __init__(self, capacity: int):
self.capacity=capacity
self.a=[]
def get(self, key: int) -> int:
for i in self.a:
if i[0]==key:
self.a.remove(i)
self.a.append(i)
return i[1]
return -1
def put(self, key: int, value: int) -> None:
for i in self.a:
if i[0]==key:
self.a.remove(i)
self.a.append((i[0],value))
return
if len(self.a)>=self.capacity:
self.a.pop(0)
self.a.append((key,value))
# Your LRUCache object will be instantiated and called as such:
# obj = LRUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)