parent
7730f3e8d4
commit
4562f8e0c1
@ -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…
Reference in new issue