From bfcc5da9f6f38c5c3881801513c5735bac06bfb3 Mon Sep 17 00:00:00 2001 From: fanxb Date: Sun, 24 Mar 2024 06:03:50 +0800 Subject: [PATCH] add --- 5.leetcode/src/com/fanxb/common/Q100.java | 33 ++++++++ 5.leetcode/src/com/fanxb/common/Q101.java | 34 ++++++++ 5.leetcode/src/com/fanxb/common/Q104.java | 34 ++++++++ 5.leetcode/src/com/fanxb/common/Q146_1.java | 88 +++++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q226.java | 36 +++++++++ 5.leetcode/src/com/fanxb/common/Q86.java | 23 ++++++ 6 files changed, 248 insertions(+) create mode 100644 5.leetcode/src/com/fanxb/common/Q100.java create mode 100644 5.leetcode/src/com/fanxb/common/Q101.java create mode 100644 5.leetcode/src/com/fanxb/common/Q104.java create mode 100644 5.leetcode/src/com/fanxb/common/Q146_1.java create mode 100644 5.leetcode/src/com/fanxb/common/Q226.java create mode 100644 5.leetcode/src/com/fanxb/common/Q86.java diff --git a/5.leetcode/src/com/fanxb/common/Q100.java b/5.leetcode/src/com/fanxb/common/Q100.java new file mode 100644 index 0000000..dc9e0e7 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q100.java @@ -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; + } + + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q101.java b/5.leetcode/src/com/fanxb/common/Q101.java new file mode 100644 index 0000000..5ee874d --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q101.java @@ -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; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q104.java b/5.leetcode/src/com/fanxb/common/Q104.java new file mode 100644 index 0000000..0f2ccff --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q104.java @@ -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)); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q146_1.java b/5.leetcode/src/com/fanxb/common/Q146_1.java new file mode 100644 index 0000000..70c8f2a --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q146_1.java @@ -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) { + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q226.java b/5.leetcode/src/com/fanxb/common/Q226.java new file mode 100644 index 0000000..5d076f2 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q226.java @@ -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); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q86.java b/5.leetcode/src/com/fanxb/common/Q86.java new file mode 100644 index 0000000..ba2a9cd --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q86.java @@ -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; + } +}