题解 | #设计LRU缓存结构#

设计LRU缓存结构

http://www.nowcoder.com/practice/e3769a5f49894d49b871c09cadd13a61

#
# lru design
# @param operators int整型二维数组 the ops
# @param k int整型 the k
# @return int整型一维数组
#
class Solution:
    def LRU(self , operators , k ):
        self.huancun = {}
        self.returnValue = []
        for i in range(len(operators)):
            if operators[i][0] == 1:
                self.setValue(operators[i],k )
            if operators[i][0] == 2:
                self.getValue(operators[i])
        return self.returnValue

    def setValue(self, list_1, k):

        if list_1[1] in self.huancun:
            self.huancun.pop(list_1[1])
            self.huancun[list_1[1]] = list_1[2]
        else:
            if len(self.huancun) == k:
                self.huancun.pop(list(self.huancun.keys())[0])
                self.huancun[list_1[1]] = list_1[2]
            else:
                self.huancun[list_1[1]] = list_1[2]
    def getValue(self,list_1):
        if list_1[1] in self.huancun:
            self.returnValue.append(self.huancun[list_1[1]])
            self.huancun.pop(list_1[1])
            self.huancun[list_1[1]] = self.returnValue[-1]
        else:
            self.returnValue.append(-1)


        # write code here
全部评论

相关推荐

06-05 19:46
已编辑
武汉大学 后端
点赞 评论 收藏
分享
评论
4
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务