xiaotiancai893661742 3 years ago
parent 4562f8e0c1
commit 4305177015

@ -20,5 +20,8 @@
</plugins> </plugins>
</build> </build>
<dependencies>
</dependencies>
</project> </project>

@ -0,0 +1,53 @@
package maslaos.shejimos;
public class Person {
int a;
int b;
int c;
int d;
private Person(){
A a = new A(){};
}
static class PersonBuilder{
Person p =new Person();
PersonBuilder basic(int a,int b){
p.a=a;
p.b=b;
return this;
}
PersonBuilder c(int c){
p.c=c;
return this;
}
PersonBuilder d(int d){
p.d=d;
return this;
}
Person builder(){
return p;
}
}
abstract class A{
// abstract void m();
}
public static void main(String[] args) {
Person person = new PersonBuilder()
.basic(1, 2)
.c(3)
.d(4)
.builder();
}
}

@ -0,0 +1,55 @@
package maslaos.shejimos;
public class Visitor {
class DianRao{
Memory m;
Cpu cpu;
public DianRao(Vi v){
m.accpect(v);
cpu.accpect(v);
}
}
abstract class DianRaoPart{
abstract void accpect(Vi v);
}
class Memory extends DianRaoPart{
@Override
void accpect(Vi v) {
v.accpectMemory(this);
}
}
class Cpu extends DianRaoPart{
@Override
void accpect(Vi v) {
v.accpectCpu(this);
}
}
interface Vi{
void accpectCpu(Cpu c);
void accpectMemory(Memory m);
}
class Avi implements Vi{
@Override
public void accpectCpu(Cpu c) {
//我在你之前
}
@Override
public void accpectMemory(Memory m) {
}
}
}

@ -0,0 +1,5 @@
package zuolaos.jichuban;
public interface A {
void a();
}

@ -1,6 +1,5 @@
package zuolaos.jichuban; package zuolaos.jichuban;
import com.sun.org.apache.regexp.internal.RE;
import java.util.*; import java.util.*;
@ -16,6 +15,39 @@ public class Code09_二叉树 {
} }
} }
private static class Info {
int height;
boolean isPing;
public Info(int height, boolean isPing) {
this.height = height;
this.isPing = isPing;
}
}
private static class Sousuo {
int max;
int min;
boolean isSousuo;
public Sousuo(boolean isSousuo, int max, int min) {
this.isSousuo = isSousuo;
this.max = max;
this.min = min;
}
}
//前序 中序 后序
public static void print(Node head) {
if (head == null) {
return;
}
print(head.left);
//中
System.out.println(head.value + " ");
print(head.right);
}
//相同结构的树判断 //相同结构的树判断
public static boolean isSameTree(Node heap1, Node heap2) { public static boolean isSameTree(Node heap1, Node heap2) {
@ -39,15 +71,6 @@ public class Code09_二叉树 {
return heap1.value == heap2.value && isSameTree(heap1.left, heap2.right) && isSameTree(heap2.right, heap2.left); 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) { public static Info isPingHengTree(Node x) {
@ -56,6 +79,10 @@ public class Code09_二叉树 {
} }
Info leftInfo = isPingHengTree(x.left); Info leftInfo = isPingHengTree(x.left);
//todo 可优化
// if (!leftInfo.isPing) {
// return new Info(0, false);
// }
Info rightInfo = isPingHengTree(x.right); Info rightInfo = isPingHengTree(x.right);
boolean isping = leftInfo.isPing && rightInfo.isPing && Math.abs(leftInfo.height - rightInfo.height) < 2; boolean isping = leftInfo.isPing && rightInfo.isPing && Math.abs(leftInfo.height - rightInfo.height) < 2;
int height = Math.max(leftInfo.height, rightInfo.height) + 1; int height = Math.max(leftInfo.height, rightInfo.height) + 1;
@ -63,6 +90,40 @@ public class Code09_二叉树 {
} }
//搜索二叉树的判断
public static Sousuo isSousuoTree(Node x) {
if (x == null) {
return null;
}
Sousuo leftNode = isSousuoTree(x.left);
Sousuo rightNode = isSousuoTree(x.right);
int max = x.value;
int min = x.value;
if (leftNode != null) {
max = Math.max(max, leftNode.max);
min = Math.min(min, leftNode.min);
}
if (rightNode != null) {
max = Math.max(max, rightNode.max);
min = Math.min(min, rightNode.min);
}
boolean isSousuo = true;
if (leftNode != null && !leftNode.isSousuo) {
isSousuo = false;
}
if (leftNode != null && !leftNode.isSousuo) {
isSousuo = false;
}
boolean leftMaxLessX = leftNode == null ? true : leftNode.max < x.value;
boolean rightMinMoreX = rightNode == null ? true : rightNode.min > x.value;
if (!(leftMaxLessX && rightMinMoreX)) {
isSousuo = false;
}
return new Sousuo(isSousuo, max, min);
}
//求最大深度 //求最大深度
public static int maxDepth(Node node) { public static int maxDepth(Node node) {
if (node == null) { if (node == null) {
@ -72,8 +133,8 @@ public class Code09_二叉树 {
} }
//先序数组 中序数组 求数 //先序数组+中序数组 构造树
public static Node maxDepth22(int[] pre, int L1, int R1, int[] in, int L2, int R2) { public static Node zhongAndXian(int[] pre, int L1, int R1, int[] in, int L2, int R2) {
if (L1 > R1) { if (L1 > R1) {
return null; return null;
} }
@ -86,8 +147,8 @@ public class Code09_二叉树 {
while (in[find] != pre[L1]) { while (in[find] != pre[L1]) {
find++; find++;
} }
head.left = maxDepth22(pre, L1 + 1, L1 + find - L2, in, L2, find - 1); head.left = zhongAndXian(pre, L1 + 1, L1 + find - L2, in, L2, find - 1);
head.right = maxDepth22(pre, L1 + find - L2 + 1, R1, in, find + 1, R2); head.right = zhongAndXian(pre, L1 + find - L2 + 1, R1, in, find + 1, R2);
return head; return head;
} }
@ -118,12 +179,53 @@ public class Code09_二叉树 {
} }
//能否组成路径和
public static boolean isZhuSum(Node node, int sum) {
//叶子节点
if (node.left == null && node.right == null) {
return node.value == sum;
}
boolean cul = node.left == null ? false : isZhuSum(node.left, sum - node.value);
cul |= node.right == null ? false : isZhuSum(node.left, sum - node.value);
return cul;
}
//组成路径和时返回路径
public static void getLuJingList(Node node, List<Integer> path, List<List<Integer>> ans, int pre, int sum) {
if (node.left == null && node.right == null) {
if (pre + node.value == sum) {
path.add(node.value);
ans.add(new ArrayList<>(path));
path.remove(path.size() - 1);
}
return;
}
pre += node.value;
path.add(node.value);
if (node.left != null) {
getLuJingList(node.left, path, ans, pre, sum);
}
if (node.right != null) {
getLuJingList(node.right, path, ans, pre, sum);
}
path.remove(path.size() - 1);
}
public static void main(String[] args) { public static void main(String[] args) {
//先 //先
int[] arr1 = {1, 2, 3, 4, 5, 6, 7}; int[] arr1 = {1, 2, 3, 4, 5, 6, 7};
int[] arr2 = {3, 2, 4, 1, 6, 5, 7}; int[] arr2 = {3, 2, 4, 1, 6, 5, 7};
Node node = maxDepth22(arr1, 0, arr1.length - 1, arr2, 0, arr2.length - 1); Node node = zhongAndXian(arr1, 0, arr1.length - 1, arr2, 0, arr2.length - 1);
System.out.println(node); System.out.println(node);
System.out.println(isPingHengTree(node).isPing); Node node1 = new Node(1);
node1.left = new Node(2);
node1.left.left = new Node(2);
node1.left.left.left = new Node(2);
node1.right = new Node(2);
System.out.println(isPingHengTree(node1).isPing);
print(node);
} }
} }

@ -0,0 +1,196 @@
package zuolaos.jichuban;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class Code10_ {
//归并排序-递归
public static void guiBing(int[] arr) {
if (arr == null || arr.length < 2) {
return;
}
guiBingProcess(arr, 0, arr.length - 1);
}
//归并排序-迭代 内部merge
public static void guiBing2(int[] arr) {
if (arr == null || arr.length < 2) {
return;
}
int step = 1;
int N = arr.length;
while (true) {
int L = 0;
int R = 0;
int mid = 0;
for (int i = 0; i < N; i += step) {
L = i;
R = (i + step - 1) < N ? (i + step - 1) : (N - 1);
mid = L + (step >> 1) - 1;
// mid = L + ((R - L) >> 1);
merge(arr, L, mid, R);
}
if (step >= N) {
break;
}
step <<= 1;
}
}
//归并排序-迭代 外部左右merge
public static void guiBing3(int[] arr) {
if (arr == null || arr.length < 2) {
return;
}
int step = 1;
int N = arr.length;
while (step < N) {
int L = 0;
while (L < N) {
int mid = 0;
if (L < N - step - 1) {
mid = L + step - 1;
} else {
mid = N - 1;
}
int R = 0;
if (mid == N - 1) {
break;
}
if (mid + step < N) {
R = mid + step;
} else {
R = N - 1;
}
merge(arr, L, mid, R);
L = R + 1;
}
if (step > (N << 1)) {
break;
}
step <<= 1;
}
}
private static void guiBingProcess(int[] arr, int L, int R) {
if (L == R) {
return;
}
int mid = L + ((R - L) >> 1);
guiBingProcess(arr, L, mid);
guiBingProcess(arr, mid + 1, R);
merge(arr, L, mid, R);
}
public static void swap(int[] arr, int i, int j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
//快速排序
public static void quickSort(int[] arr) {
if (arr == null || arr.length < 2) {
return;
}
quickSortProcess(arr, 0, arr.length - 1);
}
private static void quickSortProcess(int[] arr, int L, int R) {
if (L >= R) {
return;
}
int[] equlas = partition(arr, L, R);
quickSortProcess(arr, L, equlas[0] - 1);
quickSortProcess(arr, equlas[1] + 1, R);
}
//快速排序 - 核心方法
public static void split(int[] arr) {
int N = arr.length;
int lessXR = -1;
int moreXL = N - 1;
int index = 0;
while (index < moreXL) {
if (arr[index] < arr[N - 1]) {
lessXR++;
swap(arr, lessXR, index);
index++;
} else if (arr[index] > arr[N - 1]) {
moreXL--;
swap(arr, moreXL, index);
} else {
index++;
}
}
swap(arr, moreXL, N - 1);
}
//快速排序 - 核心方法
public static int[] partition(int[] arr, int L, int R) {
int lessXR = L - 1;
int moreXL = R;
int index = L;
while (index < moreXL) {
if (arr[index] < arr[R]) {
lessXR++;
swap(arr, lessXR, index);
index++;
} else if (arr[index] > arr[R]) {
moreXL--;
swap(arr, moreXL, index);
} else {
index++;
}
}
swap(arr, moreXL, R);
return new int[]{lessXR + 1, moreXL};
}
public void test() {
System.out.println(12);
}
//归并排序核心方法
private static void merge(int[] arr, int L, int M, int R) {
int[] help = new int[R - L + 1];
int i = 0;
int p1 = L;
int p2 = M + 1;
while (p1 <= M && p2 <= R) {
help[i++] = arr[p1] < arr[p2] ? arr[p1++] : arr[p2++];
}
while (p1 <= M) {
help[i++] = arr[p1++];
}
while (p2 <= R) {
help[i++] = arr[p2++];
}
for (int j = 0; j < help.length; j++) {
arr[L + j] = help[j];
}
}
public static void main(String[] args) throws Exception {
int[] arr = {9, 8, 7, 5, 6, 7};
// guiBing3(arr);
// split(arr);
// System.out.println(arr);
Method guiBing = Class.forName("zuolaos.jichuban.Code10_各种排序").getMethod("guiBing", int[].class);
Object invoke = guiBing.invoke(Code10_.class,arr);
System.out.println(arr);
}
}

@ -1,4 +1,13 @@
package zuolaos.jichuban; package zuolaos.jichuban;
public class Demo { public class Demo implements A {
public void a(){
System.out.println(12);
}
public static void main(String[] args) {
System.out.println(1);
}
} }

Loading…
Cancel
Save