This commit is contained in:
fanxb 2024-03-24 06:03:50 +08:00
parent b1b713df35
commit bfcc5da9f6
6 changed files with 248 additions and 0 deletions

View File

@ -0,0 +1,33 @@
package com.fanxb.common;
public class Q100 {
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) return true;
if (p != null && q != null) {
if (p.val != q.val) return false;
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
} else {
return false;
}
}
}

View File

@ -0,0 +1,34 @@
package com.fanxb.common;
public class Q101 {
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
public boolean isSymmetric(TreeNode root) {
return isSame(root.left, root.right);
}
public boolean isSame(TreeNode left, TreeNode right) {
if (left == null && right == null) return true;
if (left != null && right != null) {
if (left.val == right.val) return isSame(left.left, right.right) && isSame(left.right, right.left);
}
return false;
}
}

View File

@ -0,0 +1,34 @@
package com.fanxb.common;
public class Q104 {
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
public int maxDepth(TreeNode root) {
if (root == null) return 0;
return Math.max(maxDepth(root.left, 2), maxDepth(root.right, 2));
}
public int maxDepth(TreeNode root, int depth) {
if (root == null) {
return depth - 1;
}
return Math.max(maxDepth(root.left, depth + 1), maxDepth(root.right, depth + 1));
}
}

View File

@ -0,0 +1,88 @@
package com.fanxb.common;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* @author fanxb
* @date 2021-09-17-下午11:13
*/
public class Q146_1 {
private static class LRUCache {
private static class Node {
private Node last;
private Node next;
private int key;
private int value;
public Node() {
}
public Node(int key, int value) {
this.key = key;
this.value = value;
}
}
int capacity;
int count;
Node head;
Node end;
Node[] cache;
LRUCache(int capacity) {
this.capacity = capacity;
this.count = 0;
head = new Node();
end = new Node();
head.next = end;
end.last = head;
cache = new Node[10001];
}
int get(int key) {
Node node = cache[key];
if (node == null) return -1;
moveToHead(node);
return node.value;
}
void put(int key, int value) {
Node node = cache[key];
if (node != null) {
moveToHead(node);
node.value = value;
return;
}
node = new Node(key, value);
cache[key] = node;
node.last = head;
node.next = head.next;
head.next.last = node;
head.next = node;
count++;
if (count > capacity) {
cache[end.last.key] = null;
end.last = end.last.last;
end.last.next = end;
count--;
}
}
void moveToHead(Node node) {
node.next.last = node.last;
node.last.next = node.next;
node.next = head.next;
node.last = head;
head.next.last = node;
head.next = node;
}
}
public static void main(String[] args) {
}
}

View File

@ -0,0 +1,36 @@
package com.fanxb.common;
public class Q226 {
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
public TreeNode invertTree(TreeNode root) {
deal(root);
return root;
}
private void deal(TreeNode root) {
if (root == null) return;
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
deal(root.left);
deal(root.right);
}
}

View File

@ -0,0 +1,23 @@
package com.fanxb.common;
public class Q86 {
public ListNode partition(ListNode head, int x) {
ListNode res = new ListNode(-1);
res.next = head;
ListNode bigList = new ListNode(-1);
ListNode pre = res, cur = head, bigCur = bigList;
while (cur != null) {
if (cur.val >= x) {
bigCur.next = cur;
bigCur = cur;
pre.next = cur.next;
} else {
pre = cur;
}
cur = cur.next;
}
bigCur.next = null;
pre.next = bigList.next;
return res.next;
}
}