2024/8/27 LeetCode Hot100 LinkedList

This commit is contained in:
Cool 2024-08-27 23:59:03 +08:00
parent f905bd9bca
commit 31c3f9c0c1
5 changed files with 173 additions and 10 deletions

View File

@ -0,0 +1,162 @@
package com.cool.hot100.linkedlist;
//
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
//
///**
// * Created with IntelliJ IDEA.
// *
// * @Author: Cool
// * @Date: 2024/08/27/20:00
// * @Description:LeetCode Hot100 LinkedList最后一题
// * DayNumber 1
// * Hard 2
// * Level 7
// */
//public class LRUCache {
// private final int capacity;
//
// private Map<Integer, LRUNode> map = new HashMap<>();
// private int size = 0;
// private LRUNode sentry;
// private LRUNode curNode;
//
// public LRUCache(int capacity) {
// this.capacity = capacity;
// }
//
// public int get(int key) {
// LRUNode returnVal = map.getOrDefault(key, null);
// return returnVal == null ? -1 : returnVal.val;
// }
//
// public void put(int key, int value) {
// if (size < capacity) {
//// LRUNode lruNode = new LRUNode(value);
//// curNode.next=lruNode;
//// curNode=curNode.next;
// putList(key, value);
// if (size == 0) sentry = curNode;
//// map.put(key, lruNode);
// size++;
// } else {
// if (exist(key)) {
// putList(key, value);
// } else {
// putList(key, value);
// int val = sentry.val;
// remove(val);
// sentry = sentry.next;
// }
//
// }
// }
//
// private void remove(int key) {
// map.remove(key);
// }
//
// private boolean exist(int key) {
// return map.containsKey(key);
// }
//
// public void putList(int key, int value) {
// LRUNode node = new LRUNode(value);
// if(curNode==null){
// curNode=node;
// }else{
// curNode.next = node;
// curNode = curNode.next;
// }
// map.put(key, node);
// }
//
//}
//
//class LRUNode {
// int val;
// LRUNode next;
// LRUNode pre;
//
//
// LRUNode() {
// }
//
// LRUNode(int val) {
// this.val = val;
// }
//
// LRUNode(int val, LRUNode pre, LRUNode next) {
// this.val = val;
// this.pre = pre;
// this.next = next;
// }
//}
class LRUCache {
private static class Node {
int key, value;
Node prev, next;
Node(int k, int v) {
key = k;
value = v;
}
}
private final int capacity;
private final Node dummy = new Node(0, 0); // 哨兵节点
private final Map<Integer, Node> keyToNode = new HashMap<>();
public LRUCache(int capacity) {
this.capacity = capacity;
dummy.prev = dummy;
dummy.next = dummy;
}
public int get(int key) {
Node node = getNode(key);
return node != null ? node.value : -1;
}
public void put(int key, int value) {
Node node = getNode(key);
if (node != null) { // 有这本书
node.value = value; // 更新 value
return;
}
node = new Node(key, value); // 新书
keyToNode.put(key, node);
pushFront(node); // 放在最上面
if (keyToNode.size() > capacity) { // 书太多了
Node backNode = dummy.prev;
keyToNode.remove(backNode.key);
remove(backNode); // 去掉最后一本书
}
}
private Node getNode(int key) {
if (!keyToNode.containsKey(key)) { // 没有这本书
return null;
}
Node node = keyToNode.get(key); // 有这本书
remove(node); // 把这本书抽出来
pushFront(node); // 放在最上面
return node;
}
// 删除一个节点抽出一本书
private void remove(Node x) {
x.prev.next = x.next;
x.next.prev = x.prev;
}
// 在链表头添加一个节点把一本书放在最上面
private void pushFront(Node x) {
x.prev = dummy;
x.next = dummy.next;
x.prev.next = x;
x.next.prev = x;
}
}

View File

@ -2,11 +2,6 @@ package com.cool.hot100.linkedlist;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Num142 {

View File

@ -2,9 +2,6 @@ package com.cool.hot100.linkedlist;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class Num19 {
@ -60,4 +57,14 @@ public class Num19 {
ListNode node1 = new ListNode(2, node);
removeNthFromEnd(node1, 1);
}
@Test
public void test1(){
LRUCache cache=new LRUCache(2);
cache.put(1,1);
cache.put(2,2);
cache.get(1);
cache.put(3,3);
System.out.println(cache.get(2));
cache.put(4,4);
}
}

View File

@ -1,6 +1,5 @@
package com.cool.hot100.linkedlist;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

View File

@ -37,7 +37,7 @@ public class Num25 {
}
private ListNode[] reverse(ListNode head,ListNode next) {
private ListNode[] reverse(ListNode head, ListNode next) {
ListNode node = head;
ListNode pre = null;
ListNode nxt=next.next;