-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.py
41 lines (36 loc) · 869 Bytes
/
Solution.py
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
#coding=utf-8
__author__ = 'xuxuan'
class LRUCache(object):
_list={}
_value={}
_length=0
def __init__(self, capacity):
"""
:type capacity: int
"""
self._list=[]
self._value={}
self._length=capacity
def get(self, key):
"""
:rtype: int
"""
ans=self._value.get(key)
if ans is not None:
self.set(key,ans)
return ans
return -1
def set(self, key, value):
"""
:type key: int
:type value: int
:rtype: nothing
"""
if not self._value.get(key):
if len(self._list)==self._length:
del self._value[self._list[0]]
self._list[0:1]=[]
else:
self._list.remove(key)
self._list.append(key)
self._value[key]=value