链表递归

main
xiaotiancai893661742 3 years ago
parent 7730f3e8d4
commit 4562f8e0c1

@ -7,6 +7,18 @@
<groupId>groupId</groupId>
<artifactId>lyc_code</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

@ -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<Node> list = new ArrayList<Node>();
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<Node> list) {
if (list == null) {
return null;
}
PriorityQueue<Node> 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;

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

@ -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<List<Integer>> depthNode(Node node) {
List<List<Integer>> result = new LinkedList<>();
if (node == null) {
return result;
}
Queue<Node> queue = new LinkedList<>();
queue.add(node);
while (!queue.isEmpty()) {
List<Integer> 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);
}
}
Loading…
Cancel
Save