From 355ac33a49cf16e9a2399a0b43b92c3f3e025fb0 Mon Sep 17 00:00:00 2001 From: whaifree Date: Sat, 5 Oct 2024 23:15:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=AE=BE=E8=AE=A1=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E5=92=8C=E7=AE=97=E6=B3=95=E9=A2=98=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加了抽象工厂模式、适配器模式、桥接模式、建造者模式、组合模式、装饰器模式、享元模式、外观模式和工厂方法模式的实现 - 新增了交替打印、LeetCode算法题目(包括LCS、最长递增子序列、子序列判断、矩阵最长公共子数组等)的实现 - 优化了Semaphore实现的交替打印程序 Default Changelist AbstractFactoryPattern.java AdapterPattern.java Alternate_printing.java BridgingPattern.java BuildPattern.java ComboPattern.java DecoratorPattern.java EnjoyPattern.java FacadePattern.java FactoryMethodPattern.java FunctionInterfaceDemo.java FunctionInterfaceDemo.java LCR155.java LCR186.java LeetCode115.java LeetCode155.java LeetCode230.java LeetCode300.java LeetCode392.java LeetCode718.java LeetCode912.java PrototypePattern.java ProxyPattern.java rustDesk.yaml SocketDemo.java SpringFactoryBean.java ThreadConnect.java --- .../AbstractFactoryPattern.java | 5 +- .../CreateType/BuildPattern/BuildPattern.java | 93 ++++++++++ .../FactoryMethodPattern.java | 132 +++++++++++++++ .../PrototypePattern/PrototypePattern.java | 50 ++++++ .../AdapterPattern/AdapterPattern.java | 68 ++++++++ .../BridgingPattern/BridgingPattern.java | 84 +++++++++ .../ComboPattern/ComboPattern.java | 102 +++++++++++ .../DecoratorPattern/DecoratorPattern.java | 81 +++++++++ .../EnjoyPattern/EnjoyPattern.java | 53 ++++++ .../FacadePattern/FacadePattern.java | 79 +++++++++ .../FacadePattern/SpringFactoryBean.java | 54 ++++++ .../ProxyPattern/ProxyPattern.java | 69 ++++++++ .../whaifree/leetCode/Tree/LeetCode230.java | 48 ++++++ .../whaifree/redo/redo_all_240924/LCR155.java | 92 ++++++++++ .../whaifree/redo/redo_all_240924/LCR186.java | 51 ++++++ .../redo/redo_all_240924/LeetCode115.java | 78 +++++++++ .../redo/redo_all_240924/LeetCode155.java | 83 +++++++++ .../redo/redo_all_240924/LeetCode300.java | 48 ++++++ .../redo/redo_all_240924/LeetCode392.java | 72 ++++++++ .../redo/redo_all_240924/LeetCode718.java | 57 +++++++ .../redo/redo_all_240924/LeetCode912.java | 159 ++++++++++++++++++ .../tech/NetWorkSocket/SocketDemo.java | 9 +- .../tech/java/FunctionInterfaceDemo.java | 29 ++++ .../tech/thread/Alternate_printing.java | 57 ++++--- .../whaifree/tech/thread/ThreadConnect.java | 45 +++++ .../whaifree/tech/FunctionInterfaceDemo.java | 69 ++++++++ .../main/java/cn/whaifree/tech/rustDesk.yaml | 41 +++++ 27 files changed, 1785 insertions(+), 23 deletions(-) rename ForJdk17/src/main/java/cn/whaifree/designPattern/{ => kama/CreateType/AbstractFactoryPattern}/AbstractFactoryPattern.java (93%) create mode 100644 ForJdk17/src/main/java/cn/whaifree/designPattern/kama/CreateType/BuildPattern/BuildPattern.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/designPattern/kama/CreateType/FactoryMethodPattern/FactoryMethodPattern.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/designPattern/kama/CreateType/PrototypePattern/PrototypePattern.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/AdapterPattern/AdapterPattern.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/BridgingPattern/BridgingPattern.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/ComboPattern/ComboPattern.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/DecoratorPattern/DecoratorPattern.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/EnjoyPattern/EnjoyPattern.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/FacadePattern/FacadePattern.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/FacadePattern/SpringFactoryBean.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/ProxyPattern/ProxyPattern.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/leetCode/Tree/LeetCode230.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LCR155.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LCR186.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode115.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode155.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode300.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode392.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode718.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/tech/java/FunctionInterfaceDemo.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/tech/thread/ThreadConnect.java create mode 100644 ForJdk8/src/main/java/cn/whaifree/tech/FunctionInterfaceDemo.java create mode 100644 ForJdk8/src/main/java/cn/whaifree/tech/rustDesk.yaml diff --git a/ForJdk17/src/main/java/cn/whaifree/designPattern/AbstractFactoryPattern.java b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/CreateType/AbstractFactoryPattern/AbstractFactoryPattern.java similarity index 93% rename from ForJdk17/src/main/java/cn/whaifree/designPattern/AbstractFactoryPattern.java rename to ForJdk17/src/main/java/cn/whaifree/designPattern/kama/CreateType/AbstractFactoryPattern/AbstractFactoryPattern.java index 8186dd6..d19e417 100644 --- a/ForJdk17/src/main/java/cn/whaifree/designPattern/AbstractFactoryPattern.java +++ b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/CreateType/AbstractFactoryPattern/AbstractFactoryPattern.java @@ -1,4 +1,5 @@ -package cn.whaifree.designPattern; +package cn.whaifree.designPattern.kama.CreateType.AbstractFactoryPattern; + public class AbstractFactoryPattern { @@ -60,12 +61,12 @@ class ModernChair implements Chair{ - interface AbstractFactory{ Sofa generateSofa(); Chair generateChair(); } +// 现代工厂可以创建多种商品 class ModernFactory implements AbstractFactory { @Override diff --git a/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/CreateType/BuildPattern/BuildPattern.java b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/CreateType/BuildPattern/BuildPattern.java new file mode 100644 index 0000000..3f14789 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/CreateType/BuildPattern/BuildPattern.java @@ -0,0 +1,93 @@ +package cn.whaifree.designPattern.kama.CreateType.BuildPattern; + +import java.util.Scanner; + +/** + * + * 构造器模式, + * public BCycle build() { + * return new BCycle(this); + * } + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/4 18:18 + * @注释 + */ +public class BuildPattern { + public static void main(String[] args) { + + Scanner scanner = new Scanner(System.in); + int i = scanner.nextInt(); + for (int i1 = 0; i1 < i; i1++) { + String next = scanner.next(); + BCycle bCycle = RoadType.getBCycle(next); + System.out.println(bCycle); + } + } +} + +enum RoadType{ + MOUNTAIN("mountain",BCycle.newBuilder().setFrame("Aluminum Frame").setTries("Knobby Tires").build()), + ROAD("road",BCycle.newBuilder().setFrame("Carbon Frame").setTries("Slim Tires").build()); + + String key; + BCycle bCycle; + + RoadType(String road, BCycle build) { + this.key = road; + this.bCycle = build; + } + public static BCycle getBCycle(String key){ + for (RoadType roadType : RoadType.values()) { + if(roadType.key.equals(key)){ + return roadType.bCycle; + } + } + return null; + } +} + + + +class BCycle{ + private String frame; + private String tires; + + public BCycle(Builder builder) { + this.frame = builder.frame; + this.tires = builder.tires; + } + + static Builder newBuilder(){ + return new Builder(); + } + + + static class Builder{ + + private String frame; + private String tires; + + public BCycle build() { + return new BCycle(this); + } + + public Builder setFrame(String frame) { + this.frame = frame; + return this; + } + + public Builder setTries(String tries) { + this.tires = tries; + return this; + } + } + + @Override + public String toString() { + return frame + " " + tires; + } +} + + + diff --git a/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/CreateType/FactoryMethodPattern/FactoryMethodPattern.java b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/CreateType/FactoryMethodPattern/FactoryMethodPattern.java new file mode 100644 index 0000000..b16de1e --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/CreateType/FactoryMethodPattern/FactoryMethodPattern.java @@ -0,0 +1,132 @@ +package cn.whaifree.designPattern.kama.CreateType.FactoryMethodPattern; + +import java.util.HashMap; +import java.util.Map; +import java.util.Scanner; + +/** + * + * 工厂方法模式 + * + * 工厂方法模式 + * 单一产品:工厂方法模式主要用于创建单一类型的产品。它定义了一个创建对象的接口,但允许子类决定实例化哪一个类。 + * 单一责任原则:工厂方法模式中的工厂类只负责创建一种产品。 + * + * 扩展性:当需要增加新的产品时,需要新增具体的工厂类。 + * 代码结构: + * 定义一个创建产品的接口。 + * 具体工厂实现这个接口来创建对应的具体产品。 + * + * + * 抽象工厂模式 + * 产品族:抽象工厂模式用于创建一组相关或依赖的对象(即产品族),而无需指定它们具体的类。 + * 多产品:一个工厂可以创建多个相关的对象。 + * 扩展性:当需要增加新的产品族时,需要新增具体的工厂类;但当需要增加新的产品种类时,不需要修改现有的工厂类。 + * 代码结构: + * 定义一个创建一系列相关产品的接口。 + * 具体工厂实现这个接口来创建对应的一系列具体产品。 + * + * + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/4 16:28 + * @注释 + */ +public class FactoryMethodPattern { + + + public static void main(String[] args) { + + Scanner scanner = new Scanner(System.in); + int i = scanner.nextInt(); + for (int i1 = 0; i1 < i; i1++) { + String next = scanner.next(); + int j = scanner.nextInt(); +// for (int i2 = 0; i2 < j; i2++) { +// Context.getFactory(next).produce(); +// } + for (int k = 0; k < j; k++) { + FactoryContext.getiFactory(next).produce(); + } + } + } + +} + +enum FactoryContext { + Circle("Circle", new CircleFactory()), + Square("Square", new SquareFactory()), + ; + + String name; + IFactory iFactory; + + FactoryContext(String name, IFactory iFactory) { + this.name = name; + this.iFactory = iFactory; + } + + public static IFactory getiFactory(String key) { + for (FactoryContext value : values()) { + if (value.name.equals(key)) { + return value.iFactory; + } + } + return null; + } +} + +class Context{ + static Map map = new HashMap<>(); + static { + map.put("Circle", new CircleFactory()); + map.put("Square", new SquareFactory()); + } + + public static IFactory getFactory(String key){ + if (map.containsKey(key)) { + return map.get(key); + } + + return new IFactory() { + @Override + public Shape produce() { + System.out.println("No Factory"); + return null; + } + }; + } +} + +class Shape{ + +} + +class Circle extends Shape{ + +} +class Square extends Shape{ + +} + +interface IFactory{ + Shape produce(); +} + +class CircleFactory implements IFactory{ + + @Override + public Shape produce() { + System.out.println("Circle Block"); + return new Circle(); + } +} + +class SquareFactory implements IFactory{ + + @Override + public Shape produce() { + System.out.println("Square Block"); + return new Square(); + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/CreateType/PrototypePattern/PrototypePattern.java b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/CreateType/PrototypePattern/PrototypePattern.java new file mode 100644 index 0000000..dd76ec3 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/CreateType/PrototypePattern/PrototypePattern.java @@ -0,0 +1,50 @@ +package cn.whaifree.designPattern.kama.CreateType.PrototypePattern; + +import java.util.Scanner; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/4 18:35 + * @注释 + */ +public class PrototypePattern { + + public static void main(String[] args) throws CloneNotSupportedException { + Scanner scanner = new Scanner(System.in); + String color = scanner.next(); + int height = scanner.nextInt(); + int width = scanner.nextInt(); + int num = scanner.nextInt(); + Shape shape = new Shape(color, width, height); + + + for (int i = 0; i < num; i++) { + System.out.println(shape.clone()); + } + } +} + + +class Shape implements Cloneable{ + String color; + int width; + int height; + + @Override + public String toString() { + // Color: Red, Width: 10, Height: 5 + return "Color: " + color + ", Width: " + width + ", Height: " + height; + } + + public Shape(String color, int width, int height) { + this.color = color; + this.width = width; + this.height = height; + } + + @Override + protected Shape clone() throws CloneNotSupportedException { + return (Shape) super.clone(); + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/AdapterPattern/AdapterPattern.java b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/AdapterPattern/AdapterPattern.java new file mode 100644 index 0000000..a4f7892 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/AdapterPattern/AdapterPattern.java @@ -0,0 +1,68 @@ +package cn.whaifree.designPattern.kama.StructureType.AdapterPattern; + +import java.util.Scanner; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/4 18:58 + * @注释 + */ +public class AdapterPattern { + + + public static void main(String[] args) { + USBAdapter usbAdapter = new USBAdapter(); + + Computer computer = new Computer(); + + + + Scanner scanner = new Scanner(System.in); + int i = scanner.nextInt(); + for (int i1 = 0; i1 < i; i1++) { + int i2 = scanner.nextInt(); + if (i2 == 1) { + System.out.println(computer.inject(new TypeCInterface())); + }else if (i2 == 2){ + USBInterface usbInterface = new USBInterface(); // USB接口 + TypeCInterface adapt = usbAdapter.adapt(usbInterface);// 适配器转换为TypeC + System.out.println(computer.inject(adapt)); + } + } + } + +} + +class Computer{ + + public String inject(TypeCInterface in) { + return in.data; + } + +} + +abstract class Interface{ + String data; +} + +class USBAdapter { + + public TypeCInterface adapt(USBInterface usbInterface) { + return new TypeCInterface(usbInterface.data + " Adapter"); + } +} +class USBInterface extends TypeCInterface { + public USBInterface() { + this.data = "USB"; + } +} +class TypeCInterface extends Interface { + + public TypeCInterface() { + this.data = "TypeC"; + } + public TypeCInterface(String data) { + this.data = data; + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/BridgingPattern/BridgingPattern.java b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/BridgingPattern/BridgingPattern.java new file mode 100644 index 0000000..f82a02e --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/BridgingPattern/BridgingPattern.java @@ -0,0 +1,84 @@ +package cn.whaifree.designPattern.kama.StructureType.BridgingPattern; + +import java.util.Scanner; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/5 18:36 + * @注释 + */ +public class BridgingPattern { + + interface TEL{ + public void showBrand(); + } + static class Sony implements TEL{ + public void showBrand(){ + System.out.print("Sony TV"); + } + } + static class TCL implements TEL{ + public void showBrand(){ + System.out.print("TCL TV"); + } + } + static abstract class Operation{ + protected TEL tel; + + public void setTel(TEL tel){ + this.tel = tel; + } + + public abstract void operation(); + } + static class Open extends Operation{ + public void operation(){ + tel.showBrand(); + System.out.println(" is ON"); + } + } + static class Close extends Operation{ + public void operation(){ + tel.showBrand(); + System.out.println(" is OFF"); + } + } + static class Switch extends Operation{ + public void operation(){ + System.out.print("Switching "); + tel.showBrand(); + System.out.println(" channel"); + } + } + class Main{ + public static void main (String[] args) { + Scanner sc = new Scanner(System.in); + int nums = sc.nextInt(); + TEL tel; + Operation opera; + while(nums-- > 0){ + int type = sc.nextInt(); + int move = sc.nextInt(); + if(type == 0){ + tel = new Sony(); + }else{ + tel = new TCL(); + } + if(move == 2){ + opera = new Open(); + opera.setTel(tel); + opera.operation(); + }else if(move == 3){ + opera = new Close(); + opera.setTel(tel); + opera.operation(); + }else{ + opera = new Switch(); + opera.setTel(tel); + opera.operation(); + } + } + } + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/ComboPattern/ComboPattern.java b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/ComboPattern/ComboPattern.java new file mode 100644 index 0000000..93074ca --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/ComboPattern/ComboPattern.java @@ -0,0 +1,102 @@ +package cn.whaifree.designPattern.kama.StructureType.ComboPattern; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/5 19:11 + * @注释 + */ +public class ComboPattern { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + CompanyComposite company = new CompanyComposite(sc.next()); + DepartmentComposite departmentComposite = null; + int n = sc.nextInt(); + while(n > 0){ + String type = sc.next(); + if(type.equals("D")){ + String department = sc.next(); + departmentComposite = new DepartmentComposite(department); + company.add(departmentComposite); + }else{ + String personnel = sc.next(); + PersonnelLeaf personnelLeaf = new PersonnelLeaf(personnel); + if(departmentComposite != null) { + departmentComposite.add(personnelLeaf); + } + } + n--; + } + System.out.println("Company Structure:"); + company.print(); + } + + // Component + interface Component{ + void print(); + } + + // Leaf + static class PersonnelLeaf implements Component{ + private String name; + + public PersonnelLeaf(String name){ + this.name = name; + } + + @Override + public void print() { + System.out.println(" "+name); + } + } + + // Composite + // 部门 + static class DepartmentComposite implements Component{ + private String name; + private List list = new ArrayList<>(); + + public DepartmentComposite(String name){ + this.name = name; + } + + public void add(Component component){ + list.add(component); + } + + @Override + public void print() { + System.out.println(" " + name); + for(Component component : list){ + component.print(); + } + } + } + + // 公司-- 下面包括多个子类(部门-->员工) + static class CompanyComposite implements Component{ + private String name; + private List list = new ArrayList<>(); + + public CompanyComposite(String name){ + this.name = name; + } + + public void add(Component component){ + list.add(component); + } + + @Override + public void print() { + System.out.println(name); + for(Component component : list){ + component.print(); + } + } + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/DecoratorPattern/DecoratorPattern.java b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/DecoratorPattern/DecoratorPattern.java new file mode 100644 index 0000000..585f029 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/DecoratorPattern/DecoratorPattern.java @@ -0,0 +1,81 @@ +package cn.whaifree.designPattern.kama.StructureType.DecoratorPattern; + +import java.util.Scanner; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/5 16:43 + * @注释 + */ +public class DecoratorPattern { + + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + while (scanner.hasNext()) { + int a = scanner.nextInt(); + int b = scanner.nextInt(); + if (a == 1) { + Coffee blackCoffee = new BlackCoffee(); + CoffeeDecorator coffeeDecorator = new CoffeeDecorator(blackCoffee); + coffeeDecorator.decorate(blackCoffee, b); + } else { + Coffee latte = new Latte(); + CoffeeDecorator coffeeDecorator = new CoffeeDecorator(latte); + coffeeDecorator.decorate(latte, b); + } + } + } +} + +interface Coffee { + void addCoffeeBeans(); +} + +class BlackCoffee implements Coffee { + + @Override + public void addCoffeeBeans() { + System.out.println("Brewing Black Coffee"); + } +} + +class Latte implements Coffee { + + @Override + public void addCoffeeBeans() { + System.out.println("Brewing Latte"); + } +} + +class CoffeeDecorator { + + private final Coffee coffee; + + public CoffeeDecorator(Coffee coffee) { + this.coffee = coffee; + } + + public Coffee decorate(Coffee coffee, int add) { + if (add == 1) { + addMilk(coffee); + } else { + addSugar(coffee); + } + return coffee; + } + + public void addMilk(Coffee coffee) { + coffee.addCoffeeBeans(); + System.out.println("Adding Milk"); + } + + public void addSugar(Coffee coffee) { + coffee.addCoffeeBeans(); + System.out.println("Adding Sugar"); + } + +} + diff --git a/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/EnjoyPattern/EnjoyPattern.java b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/EnjoyPattern/EnjoyPattern.java new file mode 100644 index 0000000..d0f3331 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/EnjoyPattern/EnjoyPattern.java @@ -0,0 +1,53 @@ +package cn.whaifree.designPattern.kama.StructureType.EnjoyPattern; + +import java.util.HashMap; +import java.util.Map; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/5 19:17 + * @注释 + */ +public class EnjoyPattern { + + + class Shape{ + private String id; + private int x; + private int y; + + + public Shape(String id, int x, int y) { + this.id = id; + this.x = x; + this.y = y; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } + } + + class ShareFactory{ + + Map map; + + public ShareFactory() { + this.map = new HashMap<>(); + } + + public Shape setAndGetShape(String id,int x,int y){ + Shape shape = map.get(id); + if (shape == null) { + shape = new Shape(id, x, y); + } + return shape; + } + } + +} diff --git a/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/FacadePattern/FacadePattern.java b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/FacadePattern/FacadePattern.java new file mode 100644 index 0000000..cfd5a23 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/FacadePattern/FacadePattern.java @@ -0,0 +1,79 @@ +package cn.whaifree.designPattern.kama.StructureType.FacadePattern; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/5 18:15 + * @注释 + */ +public class FacadePattern { + // 提供了一个统一的接口,用于访问子系统中的一群接口 + + public class Facade { + private Switch ac; + private Switch lamp; + private Switch tv; + + public Facade(Switch ac, Switch lamp, Switch tv) { + this.ac = ac; + this.lamp = lamp; + this.tv = tv; + } + + public void turnOffAll() { + ac.off(); + lamp.off(); + tv.off(); + } + + public void turnOff(int o) { + switch (o) { + case 1: + ac.off(); + break; + case 2: + lamp.off(); + break; + case 3: + tv.off(); + break; + default: + System.out.println("Invalid option."); + } + } + + } + + // SubSystemClasses +// 开关 + interface Switch{ + void off(); + } + + //空调 + class AirConditioning implements Switch { + + @Override + public void off() { + System.out.println("Air Conditioner is turned off."); + } + } + + //台灯 + class DeskLamp implements Switch { + + @Override + public void off() { + System.out.println("Desk Lamp is turned off."); + } + } + + //电视机 + class Television implements Switch { + @Override + public void off() { + System.out.println("Television is turned off."); + } + } + +} diff --git a/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/FacadePattern/SpringFactoryBean.java b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/FacadePattern/SpringFactoryBean.java new file mode 100644 index 0000000..3df13a8 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/FacadePattern/SpringFactoryBean.java @@ -0,0 +1,54 @@ +package cn.whaifree.designPattern.kama.StructureType.FacadePattern; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/5 18:33 + * @注释 + */ +public class SpringFactoryBean { +} + +//class ProxyFactoryBean extends ProxyCreatorSupport implements FactoryBean, BeanClassLoaderAware, BeanFactoryAware { +// +// +// /** +// * 门面模式的获取对象 +// * @return +// * @throws BeansException +// */ +// @Nullable +// public Object getObject() throws BeansException { +// this.initializeAdvisorChain(); +// if (this.isSingleton()) { +// return this.getSingletonInstance(); +// } else { +// if (this.targetName == null) { +// this.logger.info("Using non-singleton proxies with singleton targets is often undesirable. Enable prototype proxies by setting the 'targetName' property."); +// } +// +// return this.newPrototypeInstance(); +// } +// } +// +// private synchronized Object getSingletonInstance() { +// if (this.singletonInstance == null) { +// this.targetSource = this.freshTargetSource(); +// if (this.autodetectInterfaces && this.getProxiedInterfaces().length == 0 && !this.isProxyTargetClass()) { +// Class targetClass = this.getTargetClass(); +// if (targetClass == null) { +// throw new FactoryBeanNotInitializedException("Cannot determine target class for proxy"); +// } +// +// this.setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.proxyClassLoader)); +// } +// +// super.setFrozen(this.freezeProxy); +// this.singletonInstance = this.getProxy(this.createAopProxy()); +// } +// +// return this.singletonInstance; +// } +// +//} + diff --git a/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/ProxyPattern/ProxyPattern.java b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/ProxyPattern/ProxyPattern.java new file mode 100644 index 0000000..dff37d8 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/StructureType/ProxyPattern/ProxyPattern.java @@ -0,0 +1,69 @@ +package cn.whaifree.designPattern.kama.StructureType.ProxyPattern; + +import lombok.Data; + +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; +import java.util.stream.Collectors; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/5 16:30 + * @注释 + */ +public class ProxyPattern { + + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int i = scanner.nextInt(); + Integer[] sizes = new Integer[i]; + for (int i1 = 0; i1 < i; i1++) { + sizes[i1] = scanner.nextInt(); + } + + User user = new User(); + Proxy proxy = new Proxy(sizes); + proxy.rent(user); + } + @Data + static class House{ + int size; + + public House(int size) { + this.size = size; + } + } + + static class Proxy { + + List housee; + + + // 所有房源 + public Proxy(Integer... integers) { + housee = Arrays.stream(integers).map(House::new).collect(Collectors.toList()); + } + + public void rent(User user) { + housee.forEach( + house -> { + if (house.size < 100) { + System.out.println("No"); + }else { + user.rent(); + } + } + ); + } + + } + + static class User{ + public void rent() { + System.out.println("YES"); + } + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/Tree/LeetCode230.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/Tree/LeetCode230.java new file mode 100644 index 0000000..6e85840 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/Tree/LeetCode230.java @@ -0,0 +1,48 @@ +package cn.whaifree.leetCode.Tree; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/3 17:00 + * @注释 + */ +public class LeetCode230 { + @Test + public void test() { + + + + + + TreeNode treeNode = TreeNode.constructTreeByArray(5,3,6,2,4,null,null,1); + System.out.println(new Solution().kthSmallest(treeNode, 3)); + } + + class Solution { + int res = 0; + public int kthSmallest(TreeNode root, int k) { + if (root == null) { + return -1; + } + int left = kthSmallest(root.left, k); + if (left != -1) { + return left; + } + res++; + if (res == k) { + return root.val; + } + int right = kthSmallest(root.right, k); + if (right != -1) { + return right; + } + return -1; + } + + + + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LCR155.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LCR155.java new file mode 100644 index 0000000..7dd529d --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LCR155.java @@ -0,0 +1,92 @@ +package cn.whaifree.redo.redo_all_240924; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/5 13:15 + * @注释 + */ +public class LCR155 { + + @Test + public void test() { + Solution solution = new Solution(); + Node node = new Node(4); + node.left = new Node(2); + node.right = new Node(5); + node.left.left = new Node(1); + node.left.right = new Node(3); + Node node1 = solution.treeToDoublyList(node); + System.out.println(node1); + } + + class Node { + public int val; + public Node left; + public Node right; + + public Node() {} + + public Node(int _val) { + val = _val; + } + + public Node(int _val,Node _left,Node _right) { + val = _val; + left = _left; + right = _right; + } + } + + /* +// Definition for a Node. +class Node { + public int val; + public Node left; + public Node right; + + public Node() {} + + public Node(int _val) { + val = _val; + } + + public Node(int _val,Node _left,Node _right) { + val = _val; + left = _left; + right = _right; + } +}; +*/ + class Solution { + + Node pre; + Node index; + + public Node treeToDoublyList(Node root) { + if (root == null) + return null; + pre = new Node(-1); + index = pre; // index作为链表指针 + in(root); + index.right = pre.right; + pre.right.left = index; + return pre.right; + } + + public void in(Node root) { + if (root == null) { + return; + } + + in(root.left); + index.right = root; + root.left = index; + index = index.right; + + in(root.right); + } + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LCR186.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LCR186.java new file mode 100644 index 0000000..2aeabbe --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LCR186.java @@ -0,0 +1,51 @@ +package cn.whaifree.redo.redo_all_240924; + +import org.junit.Test; + +import java.util.HashSet; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/5 14:20 + * @注释 + */ +public class LCR186 { + @Test + public void test() { + Solution solution = new Solution(); + int[] places = {0, 6, 9, 0, 7}; + System.out.println(solution.checkDynasty(places)); + } + + class Solution { + /** + * 给你5个数字,判断是否是顺子 + * @param places + * @return + */ + public boolean checkDynasty(int[] places) { +// Arrays.sort(places); + + int min = Integer.MAX_VALUE; + int max = Integer.MIN_VALUE; + HashSet set = new HashSet<>(); + for (int i = 0; i < places.length; i++) { + if (places[i] == 0) { + continue; + } + if (set.contains(places[i])) { + return false; + } else { + set.add(places[i]); + } + min = Math.min(min, places[i]); + max = Math.max(max, places[i]); + } + // 0 0 7 8 9 + // 7 0 0 10 12 + return max - min < 5; // 在这个区间,并且没有重复,则必然会顺子 + } + } + +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode115.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode115.java new file mode 100644 index 0000000..b7f0374 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode115.java @@ -0,0 +1,78 @@ +package cn.whaifree.redo.redo_all_240924; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/4 10:23 + * @注释 + */ +public class LeetCode115 { + + @Test + public void test() { + Solution solution = new Solution(); + int i = solution.numDistinct("babgbag", "bag"); + System.out.println(i); + // rabbit + int i1 = solution.numDistinct("rabbbit", "rabbit"); + System.out.println(i1); + } + + class Solution { + /** + * + * + * '' b a g + * 1 0 0 0 + * b 1 1 0 0 + * a 1 1 1 0 + * b 1 2 1 0 一样: 之前的子序列 dp[i-1][j-1] + s其他前缀在t中出现的 dp[i-1][j] + * g + * a + * g + * + * '' b a b g b a g + *'' 1 1 1 1 1 1 1 1 + * b 0 1 1 2 2 3 3 3 + * a 0 0 1 1 1 1 4 4 + * g 0 0 0 0 1 1 1 5 + * 一样:用左上角+左 表示i-1 j 中匹配的次数(这个char i用的是前面的,不用第s[i - 1]来匹配) + 本次i-1 j-1 (使用i-1进行匹配)就差这个i匹配的 + * else : 用左 + * + * '' b a b g b a g + *'' 0 0 0 0 0 0 0 0 + * b 0 1 1 2 2 3 3 3 + * a 0 0 1 1 1 1 4 4 + * g 0 0 0 0 1 1 1 5 + * + * @param s + * @param t + * @return + */ + public int numDistinct(String s, String t) { + + char[] sChar = s.toCharArray(); + char[] tChar = t.toCharArray(); + int[][] dp = new int[sChar.length + 1][tChar.length + 1]; + for (int i = 0; i < sChar.length; i++) { + dp[i][0] = 1; + } + for (int i = 1; i <= sChar.length; i++) { + for (int j = 1; j <= tChar.length; j++) { + if (sChar[i - 1] == tChar[j - 1]) { + // 如果相同 + // 使用i-1进行匹配 dp[i - 1][j - 1] bagg和bag t匹配到s的第二个g时,使用第一个g + // 不用第s[i - 1]来匹配 dp[i - 1][j] bagg和bag t匹配到s的第二个g时,不使用第一个g + dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; + } else { + // 不用s[i - 1]来匹配 + dp[i][j] = dp[i - 1][j]; + } + } + } + return dp[sChar.length][tChar.length]; + } + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode155.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode155.java new file mode 100644 index 0000000..f670f62 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode155.java @@ -0,0 +1,83 @@ +package cn.whaifree.redo.redo_all_240924; + +import org.junit.Test; + +import java.util.Deque; +import java.util.LinkedList; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/5 13:06 + * @注释 + */ +public class LeetCode155 { + + @Test + public void test() { + MinStack minStack = new MinStack(); + minStack.push(-2); + minStack.push(0); + minStack.push(-3); + System.out.println(minStack.getMin()); + minStack.pop(); + System.out.println(minStack.top()); + System.out.println(minStack.getMin()); + } + + static class Item{ + public int value; + public int nowMin; + + public Item(int value, int nowMin) { + this.value = value; + this.nowMin = nowMin; + } + + public int getNowMin() { + return nowMin; + } + + public int getValue() { + return value; + } + } + + class MinStack { + Deque stack; + public MinStack() { + stack = new LinkedList<>(); + } + + public void push(int val) { + if (stack.isEmpty()) { + stack.push(new Item(val, val)); + return; + } + Item peek = stack.peek(); + int min = Math.min(peek.nowMin, val); + stack.push(new Item(val, min)); + } + + public void pop() { + stack.pop(); + } + + public int top() { + return stack.peek().getValue(); + } + + public int getMin() { + return stack.peek().getNowMin(); + } + } + +/** + * Your MinStack object will be instantiated and called as such: + * MinStack obj = new MinStack(); + * obj.push(val); + * obj.pop(); + * int param_3 = obj.top(); + * int param_4 = obj.getMin(); + */ +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode300.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode300.java new file mode 100644 index 0000000..0558025 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode300.java @@ -0,0 +1,48 @@ +package cn.whaifree.redo.redo_all_240924; + +import java.util.Arrays; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/3 16:27 + * @注释 + */ +public class LeetCode300 { + + class Solution { + + /** + * dp[i] 表示以0-i的,最长递增子序列的长度 + * int tmp + * + * [10,9,2,5,3,7,101,18] + * 1 1 1 2 2 3 4 4 + * + * i = 0 - len + * j = i -- 0 + * + * + * 上升的增加尽可能少 + * + * @param nums + * @return + */ + public int lengthOfLIS(int[] nums) { + + int[] dp = new int[nums.length]; + Arrays.fill(dp, 1); + + for (int i = 1; i < nums.length; i++) { + // 以i结尾的最大递增长度 + for (int j = 0; j < i; j++) { + if (nums[j] < nums[i]) { + dp[i] = Math.max(dp[i], dp[j] + 1); + } + } + } + + return Arrays.stream(dp).max().getAsInt(); + } + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode392.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode392.java new file mode 100644 index 0000000..e1bcc7a --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode392.java @@ -0,0 +1,72 @@ +package cn.whaifree.redo.redo_all_240924; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/4 11:11 + * @注释 + */ +public class LeetCode392 { + + @Test + public void test() { + String s = "bb"; + String t = "ahbgdc"; + System.out.println(new Solution().isSubsequence(s, t)); + } + + class Solution { + /** + * '' a h b g d c + *'' 1 1 1 1 1 1 1 + * a 0 1 1 1 1 1 1 == 用i-1 j-1 else 左边 + * b 0 0 0 1 1 1 1 + * c 0 0 0 0 0 0 1 + * + * '' a h b g d c + *'' 1 1 1 1 1 1 1 + * a 0 1 1 1 1 1 1 + * x 0 0 0 0 0 0 0 + * c 0 0 0 0 0 0 0 + * + * + * @param s + * @param t + * @return + */ + public boolean isSubsequence(String s, String t) { + + if (s.length() == 0) { + return true; + } + if (t.length() == 0) { + return false; + } + if (s.length() == 1 && t.length() == 1 ) { + if (s.charAt(0) == t.charAt(0)) { + return true; + }else { + return false; + } + } + + boolean[][] dp = new boolean[s.length() + 1][t.length() + 1]; + Arrays.fill(dp[0], true); + + for (int i = 1; i <= s.length(); i++) { + for (int j = 1; j <= t.length(); j++) { + if (s.charAt(i - 1) == t.charAt(j - 1)) { + dp[i][j] = dp[i - 1][j - 1]; + } else { + dp[i][j] = dp[i][j - 1]; + } + } + } + return dp[s.length()][t.length()]; + } + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode718.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode718.java new file mode 100644 index 0000000..763432f --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode718.java @@ -0,0 +1,57 @@ +package cn.whaifree.redo.redo_all_240924; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/5 12:25 + * @注释 + */ +public class LeetCode718 { + + @Test + public void test() { + int[] nums1 = new int[]{0,1,1,1,1}; + int[] nums2 = new int[]{1,0,1,0,1}; + Solution solution = new Solution(); + int length = solution.findLength(nums1, nums2); + System.out.println(length); + } + + class Solution { + + /** + * 这个问题关键在于重复的数字会被重复使用 + * + * 0,1,1,1,1 + * 1 0 1 1 1 1 + * 0 1 1 1 1 1 + * 1 1 2 + * 0 + * 1 + *以下标i - 1为结尾的A” 标明一定是 以A[i-1]为结尾的字符串 + * + * @param nums1 + * @param nums2 + * @return + */ + public int findLength(int[] nums1, int[] nums2) { + + int[][] dp = new int[nums1.length + 1][nums2.length + 1]; + int max = 0; + for (int i = 1; i <= nums1.length; i++) { + for (int j = 1; j <= nums2.length; j++) { + if (nums1[i - 1] == nums2[j - 1]) { + dp[i][j] = dp[i - 1][j - 1] + 1; + } else { + dp[i][j] = 0; + } + max = Math.max(max, dp[i][j]); + } + } + return max; + } + } + +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode912.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode912.java index e39435c..8f988c8 100644 --- a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode912.java +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode912.java @@ -1,5 +1,10 @@ package cn.whaifree.redo.redo_all_240924; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Random; + /** * @version 1.0 * @Author whai文海 @@ -7,4 +12,158 @@ package cn.whaifree.redo.redo_all_240924; * @注释 */ public class LeetCode912 { + + @Test + public void test() { + int[] nums = {5,2,3,1}; + new QuickSort().sort(nums, 0, nums.length - 1); + System.out.println(Arrays.toString(nums)); + } + + class QuickSort { + + Random random = new Random(); + public int[] sortArray(int[] nums) { + sort(nums, 0, nums.length - 1); + return nums; + } + + public void sort(int[] nums, int start, int end) { + if (start >= end) { + return; + } + + int index = start + random.nextInt(end - start + 1); + swap(nums, index, end); + int base = nums[end]; + + + int l = start; + int r = end; + while (start < end) { + while (start < end && nums[start] <= base) { + start++; + } + while (start < end && nums[end] >= base) { + end--; + } +// if (start < end) { + swap(nums, start, end); +// } + } + swap(nums, start, r); + sort(nums, l, end - 1); + sort(nums, start + 1, r); + } + + public void swap(int[] nums, int i, int j) { + int temp = nums[i]; + nums[i] = nums[j]; + nums[j] = temp; + } + } + + @Test + public void test2() { + int[] nums = {5,2,3,1}; + System.out.println(Arrays.toString(new MergeSort().sortArray(nums))); + } + + class MergeSort { + public int[] sortArray(int[] nums) { + if (nums.length <= 1) { + return nums; + } + if (nums.length == 2) { + if (nums[0] > nums[1]) { + return new int[]{nums[1], nums[0]}; + }else { + return new int[]{nums[0], nums[1]}; + } + } + + int len = nums.length ; + int[] ints1 = Arrays.copyOfRange(nums, 0, len / 2); + int[] ints2 = Arrays.copyOfRange(nums, len / 2, len); + int[] sort1 = sortArray(ints1); + int[] sort2 = sortArray(ints2); + int[] sort = sort(sort1, sort2); + return sort; + } + + public int[] sort(int[] nums1, int[] nums2) { + + + + int index1 = 0; + int index2 = 0; + int index = 0; + int[] res = new int[nums1.length + nums2.length]; + while (index1 < nums1.length && index2 < nums2.length) { + if (nums1[index1] <= nums2[index2]) { + res[index++] = nums1[index1++]; + } else { + res[index++] = nums2[index2++]; + } + } + while (index1 < nums1.length) { + res[index++] = nums1[index1++]; + } + while (index2 < nums2.length) { + res[index++] = nums2[index2++]; + } + return res; + } + } + + @Test + public void test3() { + int[] nums = {-1,2,-8,-10}; + System.out.println(Arrays.toString(new HeapSort().sortArray(nums))); + } + + class HeapSort { + + + public int[] sortArray(int[] nums) { + Heap heap = new Heap(nums); + for (int i = nums.length - 1; i > 0; i--) { + heap.heapUp(i); + } + return nums; + } + + static class Heap{ + int[] heap = null; + + public Heap(int[] nums) { + heap = nums; + } + public void heapUp(int end) { + int nonLeaf = (end - 1) / 2; + while (nonLeaf >= 0) { + int left = nonLeaf * 2 + 1; + int right = nonLeaf * 2 + 2; + int maxIndex = nonLeaf; + if (left <= end && heap[left] > heap[maxIndex]) { + maxIndex = left; + } + if (right <= end && heap[right] > heap[maxIndex]) { + maxIndex = right; + } + if (nonLeaf != maxIndex) { + swap(this.heap, nonLeaf, maxIndex); + + } + nonLeaf--; + } + swap(heap, 0, end); + } + public void swap(int[] nums, int i, int j) { + int temp = nums[i]; + nums[i] = nums[j]; + nums[j] = temp; + } + } + } } diff --git a/ForJdk17/src/main/java/cn/whaifree/tech/NetWorkSocket/SocketDemo.java b/ForJdk17/src/main/java/cn/whaifree/tech/NetWorkSocket/SocketDemo.java index fac0e8b..170802d 100644 --- a/ForJdk17/src/main/java/cn/whaifree/tech/NetWorkSocket/SocketDemo.java +++ b/ForJdk17/src/main/java/cn/whaifree/tech/NetWorkSocket/SocketDemo.java @@ -255,7 +255,7 @@ class AIOServer{ // 读取客户端发送的数据 ByteBuffer buffer = ByteBuffer.allocate(1024); - client.read(buffer, buffer, new CompletionHandler() { + CompletionHandler handler = new CompletionHandler<>() { /** * 当读取操作成功完成时的处理逻辑 @@ -300,7 +300,12 @@ class AIOServer{ public void failed(Throwable exc, ByteBuffer attachment) { // 处理读取失败情况,此处未定义具体行为 } - }); + }; + + + + + client.read(buffer, buffer, handler); server.accept(null, this); } diff --git a/ForJdk17/src/main/java/cn/whaifree/tech/java/FunctionInterfaceDemo.java b/ForJdk17/src/main/java/cn/whaifree/tech/java/FunctionInterfaceDemo.java new file mode 100644 index 0000000..38ef8fe --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/tech/java/FunctionInterfaceDemo.java @@ -0,0 +1,29 @@ +package cn.whaifree.tech.java; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/3 19:10 + * @注释 + */ +public class FunctionInterfaceDemo { + + public static void main(String[] args) { + MyInterface myInterface = x -> x; + int run = myInterface.run(1); + System.out.println(run); + } + +} + +class MyClass { + public static void doSome(MyInterface myInterface) { + } + +} + + +interface MyInterface { + + abstract int run(int x); +} diff --git a/ForJdk17/src/main/java/cn/whaifree/tech/thread/Alternate_printing.java b/ForJdk17/src/main/java/cn/whaifree/tech/thread/Alternate_printing.java index 8a860cc..2ec5489 100644 --- a/ForJdk17/src/main/java/cn/whaifree/tech/thread/Alternate_printing.java +++ b/ForJdk17/src/main/java/cn/whaifree/tech/thread/Alternate_printing.java @@ -16,33 +16,52 @@ import java.util.concurrent.Semaphore; public class Alternate_printing { public static void main(String[] args) { - Semaphore s0 = new Semaphore(1); - Semaphore s1 = new Semaphore(0); + Semaphore s1 = new Semaphore(1); // permits = 1 表示 可以使用acquire对Semaphore的state--,或者使用release对Semaphore的state++ + Semaphore s2 = new Semaphore(0); + Semaphore s3 = new Semaphore(0); - new Thread(new FutureTask<>(new Callable<>() { - @Override - public Object call() throws Exception { - for (int i = 0; i < 50; i++) { - s0.acquire(); + Thread threadA = new Thread(() -> { + for (int i = 0; i < 10; i++) { + try { + s1.acquire(); // 获取信号量 System.out.println("a"); - s1.release(); + s2.release(); // 释放信号量给下一个线程 + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + System.out.println("Thread A interrupted."); } - return null; } - })).start(); + }); - - new Thread(new FutureTask<>(new Callable<>() { - @Override - public Object call() throws Exception { - for (int i = 0; i < 50; i++) { - s1.acquire(); + Thread threadB = new Thread(() -> { + for (int i = 0; i < 10; i++) { + try { + s2.acquire(); // 获取信号量 System.out.println("b"); - s0.release(); + s3.release(); // 释放信号量给下一个线程 + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + System.out.println("Thread B interrupted."); } - return null; } - })).start(); + }); + + Thread threadC = new Thread(() -> { + for (int i = 0; i < 10; i++) { + try { + s3.acquire(); // 获取信号量 + System.out.println("c"); + s1.release(); // 释放信号量给第一个线程 + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + System.out.println("Thread C interrupted."); + } + } + }); + + threadA.start(); + threadB.start(); + threadC.start(); } } diff --git a/ForJdk17/src/main/java/cn/whaifree/tech/thread/ThreadConnect.java b/ForJdk17/src/main/java/cn/whaifree/tech/thread/ThreadConnect.java new file mode 100644 index 0000000..58c2222 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/tech/thread/ThreadConnect.java @@ -0,0 +1,45 @@ +package cn.whaifree.tech.thread; + +import java.io.IOException; +import java.io.PipedInputStream; +import java.io.PipedOutputStream; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/3 18:51 + * @注释 + */ + +public class ThreadConnect { + public static void main(String[] args) throws IOException { + PipedOutputStream pipedOutputStream = new PipedOutputStream(); + PipedInputStream pipedInputStream = new PipedInputStream(pipedOutputStream); + + // 创建一个线程用于写入数据 + Thread writerThread = new Thread(() -> { + try { + pipedOutputStream.write("Hello, Piped I/O!".getBytes()); + pipedOutputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + }); + + // 创建一个线程用于读取数据 + Thread readerThread = new Thread(() -> { + try { + byte[] buffer = new byte[1024]; + int bytesRead = pipedInputStream.read(buffer); + System.out.println("Received: " + new String(buffer, 0, bytesRead)); + pipedInputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + }); + + writerThread.start(); + readerThread.start(); + } +} + diff --git a/ForJdk8/src/main/java/cn/whaifree/tech/FunctionInterfaceDemo.java b/ForJdk8/src/main/java/cn/whaifree/tech/FunctionInterfaceDemo.java new file mode 100644 index 0000000..b07b6e3 --- /dev/null +++ b/ForJdk8/src/main/java/cn/whaifree/tech/FunctionInterfaceDemo.java @@ -0,0 +1,69 @@ +package cn.whaifree.tech; + +import java.util.HashMap; +import java.util.concurrent.locks.ReentrantLock; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/3 19:18 + * @注释 + */ +public class FunctionInterfaceDemo { + static ReentrantLock lock = new ReentrantLock(); + public static void main(String[] args) throws InterruptedException { + + HashMap map = new HashMap<>(); +// map.put(null, null); + System.out.println(map.get(null)); + + new Thread(new Runnable() { + @Override + public void run() { + System.out.println("1"); + try { + lock.lock(); + throw new RuntimeException(); + } catch (Exception e) { + e.printStackTrace(); + }finally { + lock.unlock(); + } + } + }).start(); + + new Thread(new Runnable() { + @Override + public void run() { + System.out.println("2"); + try { + lock.lock(); + throw new RuntimeException(); + } catch (Exception e) { + e.printStackTrace(); + }finally { + lock.unlock(); + } + } + }).start(); + + +// MyInterface myInterface = x -> x; +// int run = myInterface.run(1); +// System.out.println(run); + } + +} + +class MyClass { + public static void doSome(MyInterface myInterface) { + } + +} + +@FunctionalInterface +interface MyInterface { + + abstract int run(int x); + +} diff --git a/ForJdk8/src/main/java/cn/whaifree/tech/rustDesk.yaml b/ForJdk8/src/main/java/cn/whaifree/tech/rustDesk.yaml new file mode 100644 index 0000000..d6fd700 --- /dev/null +++ b/ForJdk8/src/main/java/cn/whaifree/tech/rustDesk.yaml @@ -0,0 +1,41 @@ + +networks: + rustdesk-net: + external: false + +services: + hbbs: + container_name: hbbs + ports: + - 26115:21115 + - 26116:21116 # 自定义 hbbs 映射端口 + - 26116:21116/udp # 自定义 hbbs 映射端口 + image: rustdesk/rustdesk-server + command: hbbs + volumes: + - ./hbbs:/root # 自定义挂载目录 + networks: + - rustdesk-net + depends_on: + - hbbr + restart: unless-stopped + deploy: + resources: + limits: + memory: 64M + + hbbr: + container_name: hbbr + ports: + - 26117:21117 # 自定义 hbbr 映射端口 + image: rustdesk/rustdesk-server + command: hbbr + volumes: + - ./hbbr:/root # 自定义挂载目录 + networks: + - rustdesk-net + restart: unless-stopped + deploy: + resources: + limits: + memory: 64M