Skip to content

Commit 69d10b2

Browse files
committed
LRU更正
1 parent 43697d6 commit 69d10b2

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

MD/算法-数据结构-LRU淘汰算法.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,37 @@
1+
https://leetcode.com/problems/lru-cache/
2+
13
LRU,全称Least Recently Used,最近最少使用缓存。
24

35
```java
46
import java.util.HashMap;
57
import java.util.LinkedList;
68

7-
public class LRUCache2 {
9+
class LRUCache {
810
private HashMap<Integer, Integer> cacheMap = new HashMap<>();
911
private LinkedList<Integer> recentlyList = new LinkedList<>();
1012
private int capacity;
1113

12-
public LRUCache2(int capacity) {
14+
public LRUCache(int capacity) {
1315
this.capacity = capacity;
1416
}
1517

16-
private int get(int key) {
18+
public int get(int key) {
1719
if (!cacheMap.containsKey(key)) {
1820
return -1;
1921
}
20-
2122
recentlyList.remove((Integer) key);
2223
recentlyList.add(key);
23-
2424
return cacheMap.get(key);
2525
}
2626

27-
private void put(int key, int value) {
27+
public void put(int key, int value) {
2828
if (cacheMap.containsKey(key)) {
2929
recentlyList.remove((Integer) key);
30-
}
31-
32-
if (cacheMap.size() == capacity) {
30+
}else if(cacheMap.size() == capacity){
3331
cacheMap.remove(recentlyList.removeFirst());
3432
}
35-
36-
cacheMap.put(key, value);
3733
recentlyList.add(key);
38-
34+
cacheMap.put(key, value);
3935
}
4036

4137
public static void main(String[] args) {

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ PS:除开知识点,一定要准备好以下套路:
4949
* [数组-滑动窗口-最小连续子数组](https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/算法-数组-滑动窗口-最小连续子数组.md)
5050
* [数组-归并排序-合并有序数组](https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/算法-数组-归并排序-合并有序数组.md)
5151
* [链表-链表反转-链表相加](https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/算法-链表-反转链表-链表相加.md)
52-
* [算法-链表-双指针-删除倒数第n个](https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/算法-链表-双指针-删除倒数第n个.md)
52+
* [链表-双指针-删除倒数第n个](https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/算法-链表-双指针-删除倒数第n个.md)
5353
* [二叉树-二叉树反转](https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/算法-二叉树-二叉树反转.md)
5454
* [动态规划-连续子数组最大和](https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/算法-动态规划-连续子数组最大和.md)
5555
* [数据结构-LRU淘汰算法](https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/算法-数据结构-LRU淘汰算法.md)

0 commit comments

Comments
 (0)