forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAC_OrderedDict_n.py
44 lines (37 loc) · 995 Bytes
/
AC_OrderedDict_n.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
42
43
44
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author: illuz <iilluzen[at]gmail.com>
# File: AC_OrderedDict_n.py
# Create Date: 2015-03-24 12:15:26
# Usage: AC_OrderedDict_n.py
# Descripton:
import collections
class LRUCache:
# @param capacity, an integer
def __init__(self, capacity):
self.capacity = capacity
self.cache = collections.OrderedDict()
# @return an integer
def get(self, key):
if not key in self.cache:
return -1
value = self.cache.pop(key)
self.cache[key] = value
return value
# @param key, an integer
# @param value, an integer
# @return nothing
def set(self, key, value):
if key in self.cache:
self.cache.pop(key)
elif len(self.cache) == self.capacity:
self.cache.popitem(last=False)
self.cache[key] = value
# debug
l = LRUCache(2)
l.set(2, 2)
print l.get(2)
l.set(3, 3)
l.set(4, 4)
print l.get(2)
print l.get(3)