Compare commits

..

2 Commits

Author SHA1 Message Date
fanxb
5c1401ba7a add 2024-03-24 07:47:48 +08:00
fanxb
bfcc5da9f6 add 2024-03-24 07:47:47 +08:00
9 changed files with 342 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,23 @@
package com.fanxb.common;
import java.util.HashMap;
import java.util.Map;
public class Q105 {
public TreeNode buildTree(int[] preorder, int[] inorder) {
Map<Integer, Integer> map = new HashMap<>(inorder.length);
for (int i = 0; i < inorder.length; i++) map.put(inorder[i], i);
return buildTree(preorder, map, 0, preorder.length - 1, 0, inorder.length - 1);
}
public TreeNode buildTree(int[] preorder, Map<Integer, Integer> map, int pL, int pR, int iL, int iR) {
if (pL > pR) return null;
TreeNode root = new TreeNode(preorder[pL]);
int rs = map.get(root.val);
//前序左边子树结束位置
int newPr = pL + rs - iL;
root.left = buildTree(preorder, map, pL + 1, newPr, iL, rs - 1);
root.right = buildTree(preorder, map, newPr + 1, pR, rs + 1, iR);
return root;
}
}

View File

@ -0,0 +1,26 @@
package com.fanxb.common;
import java.util.HashMap;
import java.util.Map;
public class Q106 {
public TreeNode buildTree(int[] inorder, int[] postorder) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < inorder.length; i++) {
map.put(inorder[i], i);
}
return buildTree(postorder, map, 0, postorder.length - 1, 0, inorder.length - 1);
}
private TreeNode buildTree(int[] pre, Map<Integer, Integer> map, int pL, int pR, int iL, int iR) {
if (pL > pR || iL > iR) return null;
TreeNode root = new TreeNode(pre[pR]);
int index = map.get(pre[pR]);
System.out.println(index);
int newPr = pL + index - iL - 1;
root.left = buildTree(pre, map, pL, newPr, iL, index - 1);
root.right = buildTree(pre, map, newPr + 1, pR - 1, index + 1, iR);
return root;
}
}

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,45 @@
package com.fanxb.common;
public class Q82 {
public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null) return head;
ListNode res = new ListNode(), last = res, startBefore = null;
res.next = head;
while (head.next != null) {
if (head.val == head.next.val) {
if (startBefore == null)
startBefore = last;
last = head;
} else if (startBefore != null) {
last = startBefore;
startBefore.next = head.next;
startBefore = null;
} else {
last = head;
}
head = head.next;
}
if (startBefore != null) startBefore.next = null;
return res.next;
}
public ListNode better(ListNode head) {
ListNode res = new ListNode();
res.next = head;
ListNode cur = res.next, last = res;
while (cur != null && cur.next != null) {
if (cur.val == cur.next.val) {
int x = cur.next.val;
while (cur != null && cur.val == x) {
last.next = cur.next;
cur = cur.next;
}
} else {
last = cur;
cur = cur.next;
}
}
return res.next;
}
}

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;
}
}