java测试地址的通不通过_做LeetCode刷题。LFU算法老是通不过所有的测试用例

最近做Leetcode,里面有一道关于LFU的算法。但是老是通不过所有的测试用例。我感觉自己写的代码没有问题,但是他的测试用例确实又通不过(能通过大部分)。

package demo;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.LinkedHashMap;

import java.util.List;

import java.util.ListIterator;

import java.util.Map;

import java.util.Map.Entry;

import java.util.Set;

public class LFUCache {

private Map cache = null;

private Map> countRecord = null;

private int capacity = 0;

private int used = 0;

public LFUCache(int capacity) {

cache = new LinkedHashMap<>(capacity, 0.75f, true);

this.capacity = capacity;

this.countRecord = new HashMap<>();

}

public int get(int key) {

Node node = cache.get(String.valueOf(key));

if (node == null) {

return -1;

}

node.useCount++;

node.lastGetTime = System.nanoTime();

cache.put(String.valueOf(key), node);

List list = countRecord.get(node.useCount);

if (list == null || list.size() == 0) {

list = new ArrayList<>();

}

list.add(node);

countRecord.put(Long.valueOf(node.useCount), list);

return node.value;

}

public void set(int key, int value) {

used++;

if (cache.get(String.valueOf(key)) != null) {

cache.remove(String.valueOf(key));

used--;

}

Node node = new Node();

node.value = value;

node.useCount = 0;

node.lastGetTime = System.currentTimeMillis();

if (used <= this.capacity) {

cache.put(String.valueOf(key), node);

} else {

removeLast();

if (cache.size() < this.capacity) {

cache.put(String.valueOf(key), node);

}

}

}

// 淘汰最少使用的缓存

private void removeLast() {

int minCount = 0;

long getTime = 0;

int flag = 0;

String waitRemoveKey = null;

Set> set = this.cache.entrySet();

ArrayList> list = new ArrayList<>(set);

ListIterator> itera = list.listIterator();

while (itera.hasNext()) {

Map.Entry entry = itera.next();

flag++;

String key = entry.getKey();

Integer count = entry.getValue().useCount;

long lastGetTime = entry.getValue().lastGetTime;

if (flag == 1) {

minCount = count;

waitRemoveKey = key;

getTime = entry.getValue().lastGetTime;

if (minCount == 0) {

break;

}

}

if (count < minCount) {

minCount = count;

waitRemoveKey = key;

getTime = lastGetTime;

}

if (minCount == count) {

if (getTime > lastGetTime) {

minCount = count;

waitRemoveKey = key;

getTime = lastGetTime;

}

}

}

if (waitRemoveKey != null) {

this.cache.remove(waitRemoveKey);

}

}

public class Node {

public int value;

public int useCount;

public long lastGetTime;

}

public static void main(String[] args) {

LFUCache cache = new LFUCache(2);

cache.set(1, 1);

cache.set(2, 2);

cache.set(3, 3);

System.out.println(cache.get(3));

System.out.println(cache.get(2));

cache.set(4, 1);

System.out.println(cache.get(3));

System.out.println(cache.get(4));

System.out.println(cache.get(2));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值