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

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

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

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

Loading…
Cancel
Save