modify code on class

pull/6/head
左程云 4 years ago
parent 976a57dac7
commit 51edbb82ef

@ -30,9 +30,7 @@ public class Code01_IsCBT {
r = head.right;
if (
// 如果遇到了不双全的节点之后,又发现当前节点不是叶节点
(leaf && (l != null || r != null))
||
(l == null && r != null)
(leaf && (l != null || r != null)) || (l == null && r != null)
) {
return false;
@ -51,13 +49,9 @@ public class Code01_IsCBT {
}
public static boolean isCBT2(Node head) {
if (head == null) {
return true;
}
return process(head).isCBT;
}
// 对每一棵子树,是否是满二叉树、是否是完全二叉树、高度
public static class Info {
public boolean isFull;
public boolean isCBT;
@ -70,49 +64,23 @@ public class Code01_IsCBT {
}
}
public static Info process(Node X) {
if (X == null) {
public static Info process(Node x) {
if (x == null) {
return new Info(true, true, 0);
}
Info leftInfo = process(X.left);
Info rightInfo = process(X.right);
Info leftInfo = process(x.left);
Info rightInfo = process(x.right);
int height = Math.max(leftInfo.height, rightInfo.height) + 1;
boolean isFull = leftInfo.isFull
&&
rightInfo.isFull
&& leftInfo.height == rightInfo.height;
boolean isFull = leftInfo.isFull && rightInfo.isFull && leftInfo.height == rightInfo.height;
boolean isCBT = false;
if (isFull) {
if (leftInfo.isFull && rightInfo.isFull && leftInfo.height == rightInfo.height) {
isCBT = true;
} else if (leftInfo.isCBT && rightInfo.isFull && leftInfo.height == rightInfo.height + 1) {
isCBT = true;
} else if (leftInfo.isFull && rightInfo.isFull && leftInfo.height == rightInfo.height + 1) {
isCBT = true;
} else if (leftInfo.isFull && rightInfo.isCBT && leftInfo.height == rightInfo.height) {
isCBT = true;
} else { // 以x为头整棵树不满
if (leftInfo.isCBT && rightInfo.isCBT) {
if (leftInfo.isCBT
&& rightInfo.isFull
&& leftInfo.height == rightInfo.height + 1) {
isCBT = true;
}
if (leftInfo.isFull
&&
rightInfo.isFull
&& leftInfo.height == rightInfo.height + 1) {
isCBT = true;
}
if (leftInfo.isFull
&& rightInfo.isCBT && leftInfo.height == rightInfo.height) {
isCBT = true;
}
}
}
return new Info(isFull, isCBT, height);
}

@ -49,49 +49,41 @@ public class Code03_lowestAncestor {
}
}
public static Node lowestAncestor2(Node head, Node o1, Node o2) {
return process(head, o1, o2).ans;
public static Node lowestAncestor2(Node head, Node a, Node b) {
return process(head, a, b).ans;
}
// 任何子树,
public static class Info {
public boolean findA;
public boolean findB;
public Node ans;
public boolean findO1;
public boolean findO2;
public Info(Node a, boolean f1, boolean f2) {
ans = a;
findO1 = f1;
findO2 = f2;
public Info(boolean fA, boolean fB, Node an) {
findA = fA;
findB = fB;
ans = an;
}
}
public static Info process(Node X, Node o1, Node o2) {
if (X == null) {
return new Info(null, false, false);
public static Info process(Node x, Node a, Node b) {
if (x == null) {
return new Info(false, false, null);
}
Info leftInfo = process(X.left, o1, o2);
Info rightInfo = process(X.right, o1, o2);
boolean findO1 = X == o1 || leftInfo.findO1 || rightInfo.findO1;
boolean findO2 = X == o2 || leftInfo.findO2 || rightInfo.findO2;
// O1和O2最初的交汇点在哪
// 1) 在左树上已经提前交汇了
// 2) 在右树上已经提前交汇了
// 3) 没有在左树或者右树上提前交汇O1 O2 全了
// 4)
Info leftInfo = process(x.left, a, b);
Info rightInfo = process(x.right, a, b);
boolean findA = (x == a) || leftInfo.findA || rightInfo.findA;
boolean findB = (x == b) || leftInfo.findB || rightInfo.findB;
Node ans = null;
if (leftInfo.ans != null) {
ans = leftInfo.ans;
}
if (rightInfo.ans != null) {
} else if (rightInfo.ans != null) {
ans = rightInfo.ans;
}
if (ans == null) {
if (findO1 && findO2) {
ans = X;
} else {
if (findA && findB) {
ans = x;
}
}
return new Info(ans, findO1, findO2);
return new Info(findA, findB, ans);
}
// for test

@ -46,36 +46,34 @@ public class Code04_MaxHappy {
}
}
public static int maxHappy2(Employee boss) {
if (boss == null) {
return 0;
}
Info all = process2(boss);
return Math.max(all.yes, all.no);
public static int maxHappy2(Employee head) {
Info allInfo = process(head);
return Math.max(allInfo.no, allInfo.yes);
}
public static class Info {
public int yes;
public int no;
public int yes;
public Info(int y, int n) {
yes = y;
public Info(int n, int y) {
no = n;
yes = y;
}
}
public static Info process2(Employee x) {
if (x.nexts.isEmpty()) {
return new Info(x.happy, 0);
public static Info process(Employee x) {
if (x == null) {
return new Info(0, 0);
}
int yes = x.happy;
int no = 0;
int yes = x.happy;
for (Employee next : x.nexts) {
Info nextInfo = process2(next);
Info nextInfo = process(next);
no += Math.max(nextInfo.no, nextInfo.yes);
yes += nextInfo.no;
no += Math.max(nextInfo.yes, nextInfo.no);
}
return new Info(yes, no);
return new Info(no, yes);
}
// for test

@ -1,9 +1,8 @@
package class09_13;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.TreeSet;
public class Code01_LowestLexicography {
@ -11,34 +10,41 @@ public class Code01_LowestLexicography {
if (strs == null || strs.length == 0) {
return "";
}
ArrayList<String> all = new ArrayList<>();
HashSet<Integer> use = new HashSet<>();
process(strs, use, "", all);
String lowest = all.get(0);
for (int i = 1; i < all.size(); i++) {
if (all.get(i).compareTo(lowest) < 0) {
lowest = all.get(i);
TreeSet<String> ans = process(strs);
return ans.size() == 0 ? "" : ans.first();
}
// strs中所有字符串全排列返回所有可能的结果
public static TreeSet<String> process(String[] strs) {
TreeSet<String> ans = new TreeSet<>();
if (strs.length == 0) {
ans.add("");
return ans;
}
for (int i = 0; i < strs.length; i++) {
String first = strs[i];
String[] nexts = removeIndexString(strs, i);
TreeSet<String> next = process(nexts);
for (String cur : next) {
ans.add(first + cur);
}
}
return lowest;
return ans;
}
// strs里放着所有的字符串
// 已经使用过的字符串的下标在use里登记了不要再使用了
// 之前使用过的字符串,拼接成了-> path
// 用all收集所有可能的拼接结果
public static void process(String[] strs, HashSet<Integer> use, String path, ArrayList<String> all) {
if (use.size() == strs.length) {
all.add(path);
} else {
for (int i = 0; i < strs.length; i++) {
if (!use.contains(i)) {
use.add(i);
process(strs, use, path + strs[i], all);
use.remove(i);
}
// {"abc", "cks", "bct"}
// 0 1 2
// removeIndexString(arr , 1) -> {"abc", "bct"}
public static String[] removeIndexString(String[] arr, int index) {
int N = arr.length;
String[] ans = new String[N - 1];
int ansIndex = 0;
for (int i = 0; i < N; i++) {
if (i != index) {
ans[ansIndex++] = arr[i];
}
}
return ans;
}
public static class MyComparator implements Comparator<String> {
@ -91,13 +97,7 @@ public class Code01_LowestLexicography {
public static void main(String[] args) {
int arrLen = 6;
int strLen = 5;
int testTimes = 100000;
String[] arr = generateRandomStringArray(arrLen, strLen);
System.out.println("先打印一个生成的字符串");
for (String str : arr) {
System.out.print(str + ",");
}
System.out.println();
int testTimes = 10000;
System.out.println("test begin");
for (int i = 0; i < testTimes; i++) {
String[] arr1 = generateRandomStringArray(arrLen, strLen);

Loading…
Cancel
Save