parent
b9a992340e
commit
7189c8d690
@ -0,0 +1,48 @@
|
||||
package 第03期.mca_04;
|
||||
|
||||
// 给定一棵二叉树,你需要计算它的直径长度
|
||||
// 一棵二叉树的直径长度是任意两个结点路径长度中的最大值
|
||||
// 这条路径可能穿过也可能不穿过根结点
|
||||
// 测试链接 : https://leetcode.cn/problems/diameter-of-binary-tree/
|
||||
public class Code02_DiameterOfBinaryTree {
|
||||
|
||||
// 提交时不要提交这个类
|
||||
public static class TreeNode {
|
||||
public int val;
|
||||
public TreeNode left;
|
||||
public TreeNode right;
|
||||
|
||||
public TreeNode(int value) {
|
||||
val = value;
|
||||
}
|
||||
}
|
||||
|
||||
// 提交以下的方法
|
||||
public static int diameterOfBinaryTree(TreeNode root) {
|
||||
return process(root).maxDistance;
|
||||
}
|
||||
|
||||
public static class Info {
|
||||
public int maxDistance;
|
||||
public int height;
|
||||
|
||||
public Info(int m, int h) {
|
||||
maxDistance = m;
|
||||
height = h;
|
||||
}
|
||||
}
|
||||
|
||||
public static Info process(TreeNode x) {
|
||||
if (x == null) {
|
||||
return new Info(0, 0);
|
||||
}
|
||||
Info leftInfo = process(x.left);
|
||||
Info rightInfo = process(x.right);
|
||||
int height = Math.max(leftInfo.height, rightInfo.height) + 1;
|
||||
int p1 = Math.max(leftInfo.maxDistance, rightInfo.maxDistance);
|
||||
int p2 = leftInfo.height + rightInfo.height;
|
||||
int maxDistance = Math.max(p1, p2);
|
||||
return new Info(maxDistance, height);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,122 @@
|
||||
package 第03期.mca_04;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
// 本题测试链接 : https://leetcode.cn/problems/lru-cache/
|
||||
public class Code08_LRUCache {
|
||||
|
||||
// 提交以下这个类
|
||||
public class LRUCache {
|
||||
|
||||
public static class Node {
|
||||
public int key;
|
||||
public int val;
|
||||
public Node last;
|
||||
public Node next;
|
||||
|
||||
public Node(int k, int v) {
|
||||
key = k;
|
||||
val = v;
|
||||
}
|
||||
}
|
||||
|
||||
public static class DoubleLinkedList {
|
||||
private Node head;
|
||||
private Node tail;
|
||||
|
||||
public DoubleLinkedList() {
|
||||
head = null;
|
||||
tail = null;
|
||||
}
|
||||
|
||||
public void addNode(Node newNode) {
|
||||
if (newNode == null) {
|
||||
return;
|
||||
}
|
||||
if (head == null) {
|
||||
head = newNode;
|
||||
tail = newNode;
|
||||
} else {
|
||||
tail.next = newNode;
|
||||
newNode.last = tail;
|
||||
tail = newNode;
|
||||
}
|
||||
}
|
||||
|
||||
public void moveNodeToTail(Node node) {
|
||||
if (tail == node) {
|
||||
return;
|
||||
}
|
||||
if (head == node) {
|
||||
head = node.next;
|
||||
head.last = null;
|
||||
} else {
|
||||
node.last.next = node.next;
|
||||
node.next.last = node.last;
|
||||
}
|
||||
node.last = tail;
|
||||
node.next = null;
|
||||
this.tail.next = node;
|
||||
this.tail = node;
|
||||
}
|
||||
|
||||
public Node removeHead() {
|
||||
if (head == null) {
|
||||
return null;
|
||||
}
|
||||
Node ans = this.head;
|
||||
if (head == tail) {
|
||||
head = null;
|
||||
tail = null;
|
||||
} else {
|
||||
head = ans.next;
|
||||
ans.next = null;
|
||||
head.last = null;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private HashMap<Integer, Node> keyNodeMap;
|
||||
private DoubleLinkedList nodeList;
|
||||
private final int capacity;
|
||||
|
||||
public LRUCache(int cap) {
|
||||
keyNodeMap = new HashMap<>();
|
||||
nodeList = new DoubleLinkedList();
|
||||
capacity = cap;
|
||||
}
|
||||
|
||||
public int get(int key) {
|
||||
if (keyNodeMap.containsKey(key)) {
|
||||
Node ans = keyNodeMap.get(key);
|
||||
nodeList.moveNodeToTail(ans);
|
||||
return ans.val;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public void put(int key, int value) {
|
||||
if (keyNodeMap.containsKey(key)) {
|
||||
Node node = keyNodeMap.get(key);
|
||||
node.val = value;
|
||||
nodeList.moveNodeToTail(node);
|
||||
} else {
|
||||
Node newNode = new Node(key, value);
|
||||
keyNodeMap.put(key, newNode);
|
||||
nodeList.addNode(newNode);
|
||||
if (keyNodeMap.size() == capacity + 1) {
|
||||
removeMostUnusedCache();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void removeMostUnusedCache() {
|
||||
Node removeNode = nodeList.removeHead();
|
||||
keyNodeMap.remove(removeNode.key);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue