From 4562f8e0c1248689ed6059ac7de9db9a86de91f0 Mon Sep 17 00:00:00 2001 From: xiaotiancai893661742 <46739364+xiaotiancai893661742@users.noreply.github.com> Date: Sat, 9 Jul 2022 23:06:57 +0800 Subject: [PATCH] =?UTF-8?q?=E9=93=BE=E8=A1=A8=E9=80=92=E5=BD=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 12 ++ .../zuolaos/jichuban/Code07_合并链表.java | 54 +++++++- .../java/zuolaos/jichuban/Code08_位图.java | 114 ++++++++++++++++ .../zuolaos/jichuban/Code09_二叉树.java | 129 ++++++++++++++++++ 4 files changed, 307 insertions(+), 2 deletions(-) create mode 100644 src/test/java/zuolaos/jichuban/Code08_位图.java create mode 100644 src/test/java/zuolaos/jichuban/Code09_二叉树.java diff --git a/pom.xml b/pom.xml index 361699d..64950a1 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,18 @@ groupId lyc_code 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + \ No newline at end of file diff --git a/src/test/java/zuolaos/jichuban/Code07_合并链表.java b/src/test/java/zuolaos/jichuban/Code07_合并链表.java index 07c0cef..90e151e 100644 --- a/src/test/java/zuolaos/jichuban/Code07_合并链表.java +++ b/src/test/java/zuolaos/jichuban/Code07_合并链表.java @@ -1,5 +1,10 @@ package zuolaos.jichuban; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.PriorityQueue; + public class Code07_合并链表 { public static void main(String[] args) { @@ -10,11 +15,56 @@ public class Code07_合并链表 { Node head2 = new Node(1); head2.next = new Node(2); head2.next.next = new Node(9); - Node node = mergeTowLists(head1, head2); - System.out.println(node); +// Node node = mergeTowLists(head1, head2); +// System.out.println(node); + + + List list = new ArrayList(); + list.add(head1); + list.add(head2); + + +// Node result=list.get(0); +// for (int i = 1; i < list.size(); i++) { +// result=mergeTowLists(result,list.get(i)); +// } + mergeAllLists(list); + + } + + //合并k个有序链表 + private static Node mergeAllLists(List list) { + if (list == null) { + return null; + } + PriorityQueue nodes = new PriorityQueue<>((o1, o2) -> o1.value - o2.value); + for (int i = 0; i < list.size(); i++) { + if (list.get(i) != null) { + nodes.add(list.get(i)); + } + } + if (nodes.isEmpty()) { + return null; + } + + Node head = nodes.poll(); + Node pre = head; + if (pre.next != null) { + nodes.add(pre.next); + } + while (!nodes.isEmpty()) { + Node cur = nodes.poll(); + pre.next = cur; + pre = cur; + if (cur.next != null) { + nodes.add(cur.next); + } + } + return head; } + //合并两个有序链表 private static Node mergeTowLists(Node head1, Node head2) { if (head1 == null || head2 == null) { return head1 == null ? head2 : head1; diff --git a/src/test/java/zuolaos/jichuban/Code08_位图.java b/src/test/java/zuolaos/jichuban/Code08_位图.java new file mode 100644 index 0000000..817408e --- /dev/null +++ b/src/test/java/zuolaos/jichuban/Code08_位图.java @@ -0,0 +1,114 @@ +package zuolaos.jichuban; + +import com.sun.org.apache.regexp.internal.RE; + +import java.util.Arrays; + +public class Code08_位图 { + + private long[] bits; + + public Code08_位图(int max) { + bits = new long[(max >> 6) + 1]; + } + + public void add(int num) { + bits[num >> 6] |= (1L << (num & 63)); + } + + public void delete(int num) { + bits[num >> 6] &= ~(1L << (num & 63)); + } + + public boolean comtains(int num) { + return (bits[num >> 6] & (1L << (num & 63))) != 0; + } + + public static int jia(int a, int b) { + int sum = a; + while (b != 0) { + sum = a ^ b; + b = (a & b) << 1; + a = sum; + } + return sum; + } + + public static int jian(int a, int b) { + return jia(a, jia(~b, 1)); + } + + public static int cheng(int a, int b) { + int sum = 0; + while (b != 0) { + if ((b & 1) != 0) { + sum = jia(sum, a); + } + a = a << 1; + b = b >>> 1; + } + return sum; + } + + public static boolean isfu(int a) { + return a < 0; + } + + public static int bianzheng(int a) { + return jia(~a, +1); + } + + public static int chu(int a, int b) { + a = isfu(a) ? bianzheng(a) : a; + b = isfu(b) ? bianzheng(b) : b; + int ans = 0; + for (int i = 30; i >= 0; i = jia(i, 1)) { + if ((a >> i) >= b) { + ans = ans | (1 << i); + a = jian(a, b << i); + } + } + return isfu(a) ^ isfu(b) ? bianzheng(ans) : ans; + } + + + public static int zuichu(int a, int b) { + if(a==Integer.MIN_VALUE && b==Integer.MIN_VALUE){ + return 1; + }else if(b==Integer.MIN_VALUE){ + return 0; + }else if(a==Integer.MIN_VALUE){ + if(b==bianzheng(1)){ + return Integer.MAX_VALUE; + }else { + int c=chu(jia(a,1),b); + return jia(c,chu(jian(a,c),b)); + } + }else { + return chu(a,b); + } + } + + public static void main(String[] args) { + Code08_位图 code08_位图 = new Code08_位图(999); + int num = 999; + System.out.println(num >> 6); + System.out.println(1L << (num & 63)); + + code08_位图.add(456); + code08_位图.delete(456); + code08_位图.add(88); + System.out.println(code08_位图.comtains(456)); + + System.out.println(jian(25, 22)); + System.out.println(cheng(25, 3)); + + + System.out.println(false ^ false); + System.out.println(false ^ true); + int[] arr={4,5,8,6,9}; + Arrays.sort(arr); + } + + +} diff --git a/src/test/java/zuolaos/jichuban/Code09_二叉树.java b/src/test/java/zuolaos/jichuban/Code09_二叉树.java new file mode 100644 index 0000000..93e9466 --- /dev/null +++ b/src/test/java/zuolaos/jichuban/Code09_二叉树.java @@ -0,0 +1,129 @@ +package zuolaos.jichuban; + +import com.sun.org.apache.regexp.internal.RE; + +import java.util.*; + +public class Code09_二叉树 { + + private static class Node { + Node left; + Node right; + int value; + + public Node(int value) { + this.value = value; + } + } + + + //相同结构的树判断 + public static boolean isSameTree(Node heap1, Node heap2) { + if (heap1 == null ^ heap2 == null) { + return false; + } + if (heap1 == null && heap2 == null) { + return true; + } + return heap1.value == heap2.value && isSameTree(heap1.left, heap2.left) && isSameTree(heap2.right, heap2.right); + } + + //镜面树的判断 + public static boolean isJingTree(Node heap1, Node heap2) { + if (heap1 == null ^ heap2 == null) { + return false; + } + if (heap1 == null && heap2 == null) { + return true; + } + return heap1.value == heap2.value && isSameTree(heap1.left, heap2.right) && isSameTree(heap2.right, heap2.left); + } + + private static class Info { + int height; + boolean isPing; + + public Info(int height, boolean isPing) { + this.height = height; + this.isPing = isPing; + } + } + + //平衡二叉树的判断 + public static Info isPingHengTree(Node x) { + if (x == null) { + return new Info(0, true); + } + + Info leftInfo = isPingHengTree(x.left); + Info rightInfo = isPingHengTree(x.right); + boolean isping = leftInfo.isPing && rightInfo.isPing && Math.abs(leftInfo.height - rightInfo.height) < 2; + int height = Math.max(leftInfo.height, rightInfo.height) + 1; + return new Info(height, isping); + } + + + //求最大深度 + public static int maxDepth(Node node) { + if (node == null) { + return 0; + } + return Math.max(maxDepth(node.left), maxDepth(node.right)) + 1; + } + + + //先序数组 中序数组 求数 + public static Node maxDepth22(int[] pre, int L1, int R1, int[] in, int L2, int R2) { + if (L1 > R1) { + return null; + } + Node head = new Node(pre[L1]); + if (L1 == R1) { + return head; + } + int find = L2; + //todo 可优化 + while (in[find] != pre[L1]) { + find++; + } + head.left = maxDepth22(pre, L1 + 1, L1 + find - L2, in, L2, find - 1); + head.right = maxDepth22(pre, L1 + find - L2 + 1, R1, in, find + 1, R2); + return head; + } + + //按层遍历按序输出 + public static List> depthNode(Node node) { + List> result = new LinkedList<>(); + if (node == null) { + return result; + } + Queue queue = new LinkedList<>(); + queue.add(node); + while (!queue.isEmpty()) { + List ans = new LinkedList<>(); + int time = queue.size(); + for (int i = 0; i < time; i++) { + Node cur = queue.poll(); + ans.add(cur.value); + if (cur.left != null) { + queue.add(cur.left); + } + if (cur.right != null) { + queue.add(cur.right); + } + } + result.add(0, ans); + } + return result; + } + + + public static void main(String[] args) { + //先 + int[] arr1 = {1, 2, 3, 4, 5, 6, 7}; + int[] arr2 = {3, 2, 4, 1, 6, 5, 7}; + Node node = maxDepth22(arr1, 0, arr1.length - 1, arr2, 0, arr2.length - 1); + System.out.println(node); + System.out.println(isPingHengTree(node).isPing); + } +}