From 0bd6e204a72ca7f03f37e014bd940e291fef9b7c Mon Sep 17 00:00:00 2001 From: Leo <582717189@qq.com> Date: Wed, 9 Dec 2020 14:06:30 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=83=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/leo/class01_02/KM.java | 36 ++++- src/leo/class06_09/CopyListWithRandom.java | 33 ++++- src/leo/class06_09/IsPalindromeList.java | 44 +++++- .../FindFirstIntersectNode.java | 133 +++++++++++++++++- .../NodeAncestorIntersection.java | 2 +- .../{class07 => class07_10}/TraversalBT.java | 133 ++++++++++++++++-- src/leo/{class07 => class07_10}/TreeNode.java | 2 +- 7 files changed, 357 insertions(+), 26 deletions(-) rename src/leo/{class06_09 => class06_10}/FindFirstIntersectNode.java (70%) rename src/leo/{class07 => class07_10}/NodeAncestorIntersection.java (91%) rename src/leo/{class07 => class07_10}/TraversalBT.java (51%) rename src/leo/{class07 => class07_10}/TreeNode.java (90%) diff --git a/src/leo/class01_02/KM.java b/src/leo/class01_02/KM.java index 26b7e96..83e47ef 100644 --- a/src/leo/class01_02/KM.java +++ b/src/leo/class01_02/KM.java @@ -190,6 +190,40 @@ public class KM { } + public static int onlyKTime7(int[] arr, int k, int m) { + int[] t = new int[32]; + for (int num : arr) { + for (int i = 0; i < t.length; i++) { + t[i] += (num >> i) & 1; + } + } + int res = 0; + for (int i = 0; i < t.length; i++) { + if (t[i]%m!=0){ + if (t[i] % m == k) { + res |= (1 << i); + } else{ + return -1; + } + } + } + + if (res == 0) { + int count = 0; + for (int num : arr) { + if (num == 0) { + count++; + } + } + if (count == k) { + return res; + }else{ + return -1; + } + } + return res; + + } public static void main(String[] args) { int maxKinds = 20; @@ -207,7 +241,7 @@ public class KM { } int[] arr = randomArray(maxKinds, range, k, m); - int ans = onlyKTime6(arr, k, m); + int ans = onlyKTime7(arr, k, m); int ans2 = testForOnlyKTimes(arr, k, m); if (ans != ans2) { System.out.println(ans); diff --git a/src/leo/class06_09/CopyListWithRandom.java b/src/leo/class06_09/CopyListWithRandom.java index d23261f..1825bcd 100644 --- a/src/leo/class06_09/CopyListWithRandom.java +++ b/src/leo/class06_09/CopyListWithRandom.java @@ -112,6 +112,37 @@ public class CopyListWithRandom { } + public static Node copyRandomNode2(Node head) { + if (head == null) { + return null; + } + Node cur = head; + Node next; + while (cur != null) { + next = cur.next; + cur.next = new Node(cur.value); + cur.next.next = next; + cur = next; + } + cur = head; + while (cur != null) { + next = cur.next.next; + cur.next.rand = cur.rand == null ? null : cur.rand.next; + cur = next; + } + cur = head; + Node copyHead = head.next; + Node copy; + while (cur != null) { + next = cur.next.next; + copy = cur.next; + cur.next = next; + copy.next = next == null ? null : next.next; + cur = next; + } + return copyHead; + } + public static void main(String[] args){ int testTime = 1000; @@ -122,7 +153,7 @@ public class CopyListWithRandom { for (int i = 0; i < testTime; i++) { Node node = randomNode(maxSize, range); Node headMap = copyRandomNodeByMap(node); - Node head = copyRandomNode1(node); + Node head = copyRandomNode2(node); Node curNode = node; Node curHeadMap = headMap; Node curHead = head; diff --git a/src/leo/class06_09/IsPalindromeList.java b/src/leo/class06_09/IsPalindromeList.java index 68aea8a..a02079f 100644 --- a/src/leo/class06_09/IsPalindromeList.java +++ b/src/leo/class06_09/IsPalindromeList.java @@ -406,6 +406,48 @@ public class IsPalindromeList { return verify; } + public static boolean isPalindromeList8(Node head) { + if (head == null || head.next == null) { + return true; + } + Node n1 = head; + Node n2 = head; + while (n2.next != null && n2.next.next != null) { + n1 = n1.next; + n2 = n2.next.next; + } + //n1 is mid + n2 = n1.next; + n1.next = null; + Node n3 = null; + while (n2 != null) { + n3 = n2.next; + n2.next = n1; + n1 = n2; + n2 = n3; + } + n3 = n1; + n2 = head; + boolean verify = true; + while (n1 != null && n2 != null) { + if (n1.value != n2.value) { + verify = false; + break; + } + n1 = n1.next; + n2 = n2.next; + } + n2 = n3.next; + n3.next = null; + while (n2 != null) { + n1 = n2.next; + n2.next = n3; + n3 = n2; + n2 = n1; + } + return verify; + } + } @@ -424,7 +466,7 @@ class IsPalindromeListMain { Node originNode = generateRandomNode(maxSize, range); Node head = copyNode(originNode); b = IsPalindromeList.isPalindromeListByStack(head); - a = IsPalindromeList.isPalindromeList7(head); + a = IsPalindromeList.isPalindromeList8(head); if (!isEqualsNode(head, originNode)) { System.out.println("not equals node!"); } diff --git a/src/leo/class06_09/FindFirstIntersectNode.java b/src/leo/class06_10/FindFirstIntersectNode.java similarity index 70% rename from src/leo/class06_09/FindFirstIntersectNode.java rename to src/leo/class06_10/FindFirstIntersectNode.java index f87f7fc..c4cda3c 100644 --- a/src/leo/class06_09/FindFirstIntersectNode.java +++ b/src/leo/class06_10/FindFirstIntersectNode.java @@ -1,8 +1,13 @@ -package leo.class06_09; +package leo.class06_10; -import com.sun.org.apache.regexp.internal.REUtil; -import com.sun.xml.internal.bind.v2.model.core.ID; +class Node{ + int value; + Node next; + public Node(int v){ + this.value = v; + } +} /** * @author Leo * @ClassName FindFirstIntersectNode @@ -243,6 +248,119 @@ class FindFirstIntersectNode1{ } } +class FindFirstIntersectNode2{ + + public static Node getIntersectNode(Node head1,Node head2){ + if (head1 == null || head1.next == null || head1.next.next == null) { + return null; + } + if (head2 == null || head2.next == null || head2.next.next == null) { + return null; + } + Node loop1 = getLoop(head1); + Node loop2 = getLoop(head2); + if (loop1 != null && loop2 != null) { + return bothLoop(head1, loop1, head2, loop2); + } else if (loop1 == null && loop2 == null) { + return noLoop(head1, head2); + + } + return null; + + } + + public static Node getLoop(Node head) { + if (head == null || head.next == null || head.next.next == null) { + return null; + } + Node slow = head.next; + Node fast = head.next.next; + while (slow != fast) { + if (fast.next == null || fast.next.next == null) { + return null; + } + slow = slow.next; + fast = fast.next.next; + } + fast = head; + while(fast!=slow){ + slow = slow.next; + fast = fast.next; + } + return slow; + } + + private static Node noLoop(Node head1, Node head2) { + int n = 0; + Node cur1 = head1; + Node cur2 = head2; + while (cur1 != null) { + cur1 = cur1.next; + n++; + } + while (cur2 != null) { + cur2 = cur2.next; + n--; + } + if (cur1 != cur2) { + return null; + } + cur1 = n > 0 ? head1 : head2; + cur2 = cur1 == head1 ? head2 : head1; + n = Math.abs(n); + while (n != 0) { + n--; + cur1 = cur1.next; + } + while (cur1 != cur2) { + cur1 = cur1.next; + cur2 = cur2.next; + } + return cur1; + } + + private static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) { + + if (loop1 != loop2) { + Node cur = loop1.next; + while (cur != loop1) { + if (cur == loop2) { + return loop1; + } + cur = cur.next; + } + return null; + }else{ + Node cur1 = head1; + Node cur2 = head2; + int n = 0; + while (cur1 != loop1) { + n++; + cur1 = cur1.next; + } + while (cur2 != loop2) { + n--; + cur2 = cur2.next; + } + if (cur1 != cur2) { + return null; + } + cur1 = n > 0 ? head1 : head2; + cur2 = cur1 == head1 ? head2 : head1; + n = Math.abs(n); + while (n != 0) { + cur1 = cur1.next; + n--; + } + while (cur1 != cur2) { + cur1 = cur1.next; + cur2 = cur2.next; + } + return cur1; + } + } +} + class FindFirstIntersectNode_Main{ public static void main(String[] args) { // 1->2->3->4->5->6->7->null @@ -259,7 +377,7 @@ class FindFirstIntersectNode_Main{ head2.next = new Node(9); head2.next.next = new Node(8); head2.next.next.next = head1.next.next.next.next.next; // 8->6 - System.out.println(FindFirstIntersectNode1.getIntersectNode(head1, head2).value); + System.out.println(FindFirstIntersectNode2.getIntersectNode(head1, head2).value); // 1->2->3->4->5->6->7->4... head1 = new Node(1); @@ -276,14 +394,17 @@ class FindFirstIntersectNode_Main{ head2.next = new Node(9); head2.next.next = new Node(8); head2.next.next.next = head1.next; // 8->2 - System.out.println(FindFirstIntersectNode1.getIntersectNode(head1, head2).value); + System.out.println(FindFirstIntersectNode2.getIntersectNode(head1, head2).value); // 0->9->8->6->4->5->6.. head2 = new Node(0); head2.next = new Node(9); head2.next.next = new Node(8); head2.next.next.next = head1.next.next.next.next.next; // 8->6 - System.out.println(FindFirstIntersectNode1.getIntersectNode(head1, head2).value); + System.out.println(FindFirstIntersectNode2.getIntersectNode(head1, head2).value); } } + + + diff --git a/src/leo/class07/NodeAncestorIntersection.java b/src/leo/class07_10/NodeAncestorIntersection.java similarity index 91% rename from src/leo/class07/NodeAncestorIntersection.java rename to src/leo/class07_10/NodeAncestorIntersection.java index a390484..6e0f18b 100644 --- a/src/leo/class07/NodeAncestorIntersection.java +++ b/src/leo/class07_10/NodeAncestorIntersection.java @@ -1,4 +1,4 @@ -package leo.class07; +package leo.class07_10; /** * @author Leo diff --git a/src/leo/class07/TraversalBT.java b/src/leo/class07_10/TraversalBT.java similarity index 51% rename from src/leo/class07/TraversalBT.java rename to src/leo/class07_10/TraversalBT.java index 5d672af..8445d29 100644 --- a/src/leo/class07/TraversalBT.java +++ b/src/leo/class07_10/TraversalBT.java @@ -1,4 +1,4 @@ -package leo.class07; +package leo.class07_10; import java.util.Stack; @@ -15,10 +15,6 @@ import java.util.Stack; */ public class TraversalBT { - - - - public static void main(String[] args){ TreeNode head = new TreeNode(1); head.left = new TreeNode(2); @@ -28,18 +24,19 @@ public class TraversalBT { head.right.left = new TreeNode(6); head.right.right = new TreeNode(7); - Recursive.pre(head); + Recursive.pre1(head); System.out.println(); - UnRecursive.pre(head); + UnRecursive.pre1(head); System.out.println("--------"); - Recursive.in(head); + + Recursive.in1(head); System.out.println(); - UnRecursive.in(head); + UnRecursive.in1(head); System.out.println("---------"); - Recursive.pos(head); - System.out.println(); - UnRecursive.pos(head); + Recursive.pos1(head); + System.out.println(); + UnRecursive.pos1(head); System.out.println("---------"); } @@ -62,6 +59,15 @@ class Recursive{ pre(node.right); } + public static void pre1(TreeNode node) { + if (node == null) { + return; + } + System.out.print(node.value+" "); + pre1(node.left); + pre1(node.right); + } + public static void in(TreeNode node) { if (node == null) { return; @@ -70,6 +76,14 @@ class Recursive{ System.out.print(node.value+" "); in(node.right); } + public static void in1(TreeNode node){ + if (node == null) { + return; + } + in1(node.left); + System.out.print(node.value+" "); + in1(node.right); + } public static void pos(TreeNode node) { if (node == null) { @@ -80,6 +94,14 @@ class Recursive{ System.out.print(node.value+" "); } + public static void pos1(TreeNode node) { + if (node == null) { + return; + } + pos1(node.left); + pos1(node.right); + System.out.print(node.value + " "); + } } @@ -93,7 +115,7 @@ class UnRecursive{ if (head == null) { return; } - System.out.print("pre-order: "); + System.out.println("pre-order: "); Stack stack = new Stack<>(); stack.add(head); while (!stack.isEmpty()) { @@ -109,6 +131,26 @@ class UnRecursive{ System.out.println(); } + public static void pre1(TreeNode head){ + if (head == null) { + return; + } + Stack stack = new Stack<>(); + stack.push(head); + while (!stack.isEmpty()) { + TreeNode cur = stack.pop(); + System.out.print(cur.value+" "); + if (cur.right != null) { + stack.push(cur.right); + } + if (cur.left != null) { + stack.push(cur.left); + } + + } + System.out.println(); + } + /** * 中序:左头右 */ @@ -116,7 +158,7 @@ class UnRecursive{ if (head == null) { return; } - System.out.print("in-order: "); + System.out.println("in-order: "); Stack stack = new Stack<>(); TreeNode cur = head; while (cur != null || !stack.isEmpty()) { @@ -133,7 +175,45 @@ class UnRecursive{ } + public static void in1(TreeNode head){ + if (head == null) { + return; + } + Stack stack = new Stack<>(); + TreeNode cur = head; + while (!stack.isEmpty() || cur != null) { + if (cur != null) { + stack.push(cur); + cur = cur.left; + } else { + cur = stack.pop(); + System.out.print(cur.value + " "); + cur = cur.right; + } + + } + System.out.println(); + + } + public static void in2(TreeNode head) { + if (head == null) { + return; + } + Stack stack = new Stack<>(); + TreeNode cur = head; + while (!stack.isEmpty() || cur != null) { + if (cur != null) { + stack.push(cur); + cur = cur.left; + }else{ + cur = stack.pop(); + System.out.print(cur.value + " "); + cur = cur.right; + } + } + System.out.println(); + } /** * 左右头 @@ -142,7 +222,7 @@ class UnRecursive{ if (head == null) { return; } - System.out.print("pos-order: "); + System.out.println("pos-order: "); Stack s1 = new Stack<>(); Stack s2 = new Stack<>(); TreeNode cur = head; @@ -164,6 +244,29 @@ class UnRecursive{ } + public static void pos1(TreeNode head){ + if (head == null) { + return; + } + Stack s1 = new Stack<>(); + Stack s2 = new Stack<>(); + TreeNode cur = head; + s1.push(cur); + while (!s1.isEmpty()) { + cur = s1.pop(); + s2.push(cur); + if (cur.left != null) { + s1.push(cur.left); + } + if (cur.right != null) { + s1.push(cur.right); + } + } + while (!s2.isEmpty()) { + System.out.print(s2.pop().value + " "); + } + System.out.println(); + } } diff --git a/src/leo/class07/TreeNode.java b/src/leo/class07_10/TreeNode.java similarity index 90% rename from src/leo/class07/TreeNode.java rename to src/leo/class07_10/TreeNode.java index b8417e2..184c74f 100644 --- a/src/leo/class07/TreeNode.java +++ b/src/leo/class07_10/TreeNode.java @@ -1,4 +1,4 @@ -package leo.class07; +package leo.class07_10; /** * @author Leo