Compare commits
10 Commits
b8cbf111fe
...
355ac33a49
Author | SHA1 | Date | |
---|---|---|---|
355ac33a49 | |||
00672efdc8 | |||
a1524dde27 | |||
0f6a5b723f | |||
64ea4c89f0 | |||
574e99f373 | |||
61d92b60d5 | |||
2219024e1b | |||
6d49b27147 | |||
33dffb6b07 |
76
ForJdk17/pom.xml
Normal file
76
ForJdk17/pom.xml
Normal file
@ -0,0 +1,76 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.example</groupId>
|
||||
<artifactId>LeetCode</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>ForJdk17</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>ForJdk17</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- Lombok -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.30</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
<!-- JPA API -->
|
||||
<dependency>
|
||||
<groupId>javax.persistence</groupId>
|
||||
<artifactId>javax.persistence-api</artifactId>
|
||||
<version>2.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.phantomthief</groupId>
|
||||
<artifactId>simple-pool</artifactId>
|
||||
<version>0.1.17</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!--spring-->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>5.3.18</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cglib</groupId>
|
||||
<artifactId>cglib</artifactId>
|
||||
<version>3.3.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-http</artifactId>
|
||||
<version>5.8.18</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,58 @@
|
||||
package cn.whaifree.designPattern.Factory;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/9/22 21:14
|
||||
* @注释
|
||||
*/
|
||||
public class FactoryAndStrategy {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Factory factory = new Factory();
|
||||
Pay pay = factory.getPay(PayType.WX);
|
||||
pay.pay(100);
|
||||
Pay pay2 = factory.getPay(PayType.WX);
|
||||
pay2.pay(200);
|
||||
|
||||
}
|
||||
|
||||
interface Pay{
|
||||
void pay(int price);
|
||||
}
|
||||
|
||||
static class WxPay implements Pay {
|
||||
@Override
|
||||
public void pay(int price) {
|
||||
System.out.println("微信支付:" + price);
|
||||
}
|
||||
}
|
||||
static class AliPay implements Pay {
|
||||
|
||||
@Override
|
||||
public void pay(int price) {
|
||||
System.out.println("支付宝支付:" + price);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static class Factory{
|
||||
public Pay getPay(PayType payType){
|
||||
return payType.pay;
|
||||
}
|
||||
}
|
||||
|
||||
enum PayType {
|
||||
WX(new WxPay()),
|
||||
ALI(new AliPay()), // 单例
|
||||
;
|
||||
|
||||
private Pay pay;
|
||||
|
||||
PayType(Pay pay) {
|
||||
this.pay = pay;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -3,12 +3,41 @@ package cn.whaifree.designPattern;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Scanner;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public class SingletonPattern {
|
||||
|
||||
static class LazySingleton {
|
||||
private static LazySingleton instance;
|
||||
|
||||
private LazySingleton() {
|
||||
}
|
||||
|
||||
public static LazySingleton getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new LazySingleton();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
static class HungrySingleton {
|
||||
private static final HungrySingleton instance = new HungrySingleton();
|
||||
|
||||
private HungrySingleton() {
|
||||
}
|
||||
|
||||
public static HungrySingleton getInstance() {
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
class Main{
|
||||
public static void main(String[] args) {
|
@ -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
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -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<String, IFactory> 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();
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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<Component> 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<Component> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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<String, Shape> 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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<Object>, 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;
|
||||
// }
|
||||
//
|
||||
//}
|
||||
|
@ -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<House> 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");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package cn.whaifree.interview.HKWS;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/10/1 11:25
|
||||
* @注释
|
||||
*/
|
||||
public class P241001 {
|
||||
public static void main(String[] args){
|
||||
Scanner in = new Scanner(System.in);
|
||||
|
||||
int left;
|
||||
left = Integer.parseInt(in.nextLine().trim());
|
||||
|
||||
int right;
|
||||
right = Integer.parseInt(in.nextLine().trim());
|
||||
|
||||
int[] res = new P241001().getPrimes(left, right);
|
||||
if(res==null||res.length==0){
|
||||
System.out.print("-1");
|
||||
}
|
||||
for(int res_i=0; res_i < res.length; res_i++) {
|
||||
System.out.print(String.valueOf(res[res_i])+" ");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* Write Code Here */
|
||||
public int[] getPrimes(int left, int right) {
|
||||
|
||||
List<Integer> res = new ArrayList<>();
|
||||
while (left <= right) {
|
||||
if (isPrime(left)) {
|
||||
res.add(left);
|
||||
}
|
||||
left++;
|
||||
}
|
||||
int[] re = new int[res.size()];
|
||||
for (int i = 0; i < re.length; i++) {
|
||||
re[i] = res.get(i);
|
||||
}
|
||||
return re;
|
||||
}
|
||||
|
||||
public static boolean isPrime(int n) {
|
||||
if (n <= 1) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 2; i * i <= n; i++) {
|
||||
if ( n % i == 0){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
64
ForJdk17/src/main/java/cn/whaifree/interview/HS/Qz/P1.java
Normal file
64
ForJdk17/src/main/java/cn/whaifree/interview/HS/Qz/P1.java
Normal file
@ -0,0 +1,64 @@
|
||||
package cn.whaifree.interview.HS.Qz;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/9/26 18:56
|
||||
* @注释
|
||||
*/
|
||||
public class P1 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// a + b = c 一只c 求 a*b 的最大值
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
long i = scanner.nextLong();
|
||||
long maxProduct = 0;
|
||||
for (int j = 0; j <= i; j++) {
|
||||
long b = i - j;
|
||||
long product = b * j;
|
||||
maxProduct = Math.max(product, maxProduct);
|
||||
}
|
||||
System.out.println(maxProduct);
|
||||
}
|
||||
}
|
||||
|
||||
class p2{
|
||||
public static void main(String[] args) {
|
||||
|
||||
Scanner in = new Scanner(System.in);
|
||||
// 注意 hasNext 和 hasNextLine 的区别
|
||||
int n = in.nextInt();
|
||||
int k = in.nextInt();
|
||||
if (n <= 1) {
|
||||
System.out.println(-1);
|
||||
return;
|
||||
}
|
||||
int[] nums = new int[n];
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
nums[i] = in.nextInt();
|
||||
}
|
||||
|
||||
int tmp = 0;
|
||||
for (int i = 1; i < nums.length; i++) {
|
||||
int search = search(nums[i], nums[i - 1]);
|
||||
if (search != -1) {
|
||||
tmp = Math.max(search, tmp);
|
||||
}
|
||||
}
|
||||
System.out.println(tmp);
|
||||
|
||||
}
|
||||
|
||||
public static int search(int a, int b) {
|
||||
int tmp = Math.min(a, b);
|
||||
while (tmp > 0) {
|
||||
if (a % tmp == 0 && b % tmp == 0) {
|
||||
return tmp;
|
||||
}
|
||||
tmp--;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package cn.whaifree.interview.qz.huachen;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/9/19 18:55
|
||||
* @注释
|
||||
*/
|
||||
public class P1 {
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package cn.whaifree.interview.qz.suiyou;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/9/24 19:18
|
||||
* @注释
|
||||
*/
|
||||
public class p1 {
|
||||
}
|
137
ForJdk17/src/main/java/cn/whaifree/interview/qz/zzyc/p1.java
Normal file
137
ForJdk17/src/main/java/cn/whaifree/interview/qz/zzyc/p1.java
Normal file
@ -0,0 +1,137 @@
|
||||
package cn.whaifree.interview.qz.zzyc;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/9/20 0:35
|
||||
* @注释
|
||||
*/
|
||||
public class p1 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
List<Integer> list = Arrays.asList(0, 11, 12, 13,1);
|
||||
System.out.println(new p1().isStraight(new ArrayList<>(list)));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* <img src="https://pic.leetcode-cn.com/1599885716-MGMODX-Picture1.png"/>
|
||||
* 1. 排序
|
||||
*
|
||||
*
|
||||
* 给定的5张牌是否是顺子
|
||||
* @param nums int整型ArrayList 扑克牌对应的数字集合
|
||||
* @return bool布尔型
|
||||
*/
|
||||
public boolean isStraight (ArrayList<Integer> nums) {
|
||||
// write code here
|
||||
|
||||
Collections.sort(nums);
|
||||
|
||||
|
||||
if (inEdge(nums)) { // 如果出现1 10 11 12 13 这种情况
|
||||
return true;
|
||||
}
|
||||
|
||||
int joker = -1;
|
||||
for (int i = 0; i < nums.size() - 1; i++) {
|
||||
if (nums.get(i) == 0) {
|
||||
joker++;
|
||||
} else if (nums.get(i).equals(nums.get(i + 1))) {
|
||||
return false; // 排序后出现重复,不可能是顺子
|
||||
}
|
||||
}
|
||||
|
||||
// 比如 5 4 3 2 1 最大的减去最小在范围内
|
||||
return nums.get(4) - nums.get(joker + 1) < 5;
|
||||
}
|
||||
|
||||
|
||||
static ArrayList<ArrayList<Integer>> o = new ArrayList<>();
|
||||
static {
|
||||
o.add(new ArrayList<>(Arrays.asList(0, 11, 12, 13, 1)));
|
||||
o.add(new ArrayList<>(Arrays.asList(0, 10, 12, 13, 1)));
|
||||
o.add(new ArrayList<>(Arrays.asList(0, 10, 11, 13, 1)));
|
||||
o.add(new ArrayList<>(Arrays.asList(0, 10, 11, 12, 1)));
|
||||
o.add(new ArrayList<>(Arrays.asList(0, 10, 11, 12, 13)));
|
||||
o.add(new ArrayList<>(Arrays.asList(1, 10, 11, 12, 13)));
|
||||
}
|
||||
public static boolean inEdge(ArrayList<Integer> list) {
|
||||
for (ArrayList<Integer> integers : o) {
|
||||
if (integers.equals(list)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
class Solution {
|
||||
public boolean checkDynasty(int[] places) {
|
||||
Set<Integer> repeat = new HashSet<>();
|
||||
int max = 0, min = 14;
|
||||
for(int place : places) {
|
||||
if(place == 0) continue; // 跳过未知朝代
|
||||
max = Math.max(max, place); // 最大编号朝代
|
||||
min = Math.min(min, place); // 最小编号朝代
|
||||
if(repeat.contains(place)) {
|
||||
return false; // 若有重复,提前返回 false
|
||||
}
|
||||
repeat.add(place); // 添加此朝代至 Set
|
||||
}
|
||||
return max - min < 5; // 最大编号朝代 - 最小编号朝代 < 5 则连续
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
class P{
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<Integer> list = Arrays.asList(1, 4, 6, 6, 11);
|
||||
|
||||
System.out.println(prize(new ArrayList<>(list), 12));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
|
||||
*
|
||||
*
|
||||
* @param res int整型ArrayList
|
||||
* @param target int整型
|
||||
* @return int整型
|
||||
*/
|
||||
public static int prize (ArrayList<Integer> res, int target) {
|
||||
// write code here
|
||||
|
||||
// for (int i = 0; i < res.size(); i++) {
|
||||
// if (res.get(i) >= target) {
|
||||
// return i;
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
int left = 0;
|
||||
int right = res.size() - 1;
|
||||
while (left <= right) {
|
||||
int mid = (left + right) / 2;
|
||||
if (res.get(mid) > target) {
|
||||
right = mid - 1;
|
||||
} else if (res.get(mid) == target) {
|
||||
return mid + 1;
|
||||
}else {
|
||||
left = mid + 1;
|
||||
}
|
||||
}
|
||||
if (left < res.size() && res.get(left) > target) {
|
||||
return left + 1;
|
||||
}
|
||||
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package cn.whaifree.leetCode.Array;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/9/19 23:02
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode167 {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
Solution1 solution = new Solution1();
|
||||
int[] ints = solution.twoSum(new int[]{2,7,11,15}, 9);
|
||||
System.out.println(ints[0] + " " + ints[1]);
|
||||
}
|
||||
|
||||
class Solution {
|
||||
public int[] twoSum(int[] numbers, int target) {
|
||||
|
||||
for (int i = 0; i < numbers.length; i++) {
|
||||
int tar = target - numbers[i];
|
||||
int j = binarySearch(numbers, tar);
|
||||
if (j != -1 && i != j) {
|
||||
return i < j ? new int[]{i + 1, j + 1} : new int[]{j + 1, i + 1};
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int binarySearch(int[] nums, int target) {
|
||||
int left = 0;
|
||||
int right = nums.length - 1;
|
||||
while (left <= right) {
|
||||
int mid = (left + right) / 2;
|
||||
if (nums[mid] == target) {
|
||||
return mid;
|
||||
} else if (nums[mid] < target) {
|
||||
left = mid + 1;
|
||||
} else {
|
||||
right = mid - 1;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
class Solution1 {
|
||||
public int[] twoSum(int[] numbers, int target) {
|
||||
int left = 0;
|
||||
int right = numbers.length - 1;
|
||||
while (left < right) {
|
||||
int sum = numbers[left] + numbers[right];
|
||||
if (sum == target) {
|
||||
return new int[]{left + 1, right + 1};
|
||||
} else if (sum < target) {
|
||||
left++;
|
||||
}else {
|
||||
right--;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
package cn.whaifree.leetCode.Array;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.AsynchronousFileChannel;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/9/22 14:56
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode228 {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(Paths.get("D:\\project\\LeetCode\\README.md"), StandardOpenOption.READ);
|
||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||
Future<Integer> result = fileChannel.read(buffer, 0);
|
||||
while (!result.isDone()) {
|
||||
// do something
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
int[] nums = {0, 2};
|
||||
System.out.println(new Solution().summaryRanges(nums));
|
||||
}
|
||||
|
||||
class Solution {
|
||||
|
||||
// public List<String> summaryRanges(int[] nums) {
|
||||
// List<String> path = new ArrayList<>();
|
||||
// List<String> res = new ArrayList<>();
|
||||
// for (int i = 1; i < nums.length; i++) {
|
||||
// if (nums[i] != nums[i - 1]) {
|
||||
// StringBuilder str = new StringBuilder();
|
||||
// for (int j = 0; j < path.size()-1; j++) {
|
||||
// str.append(path.get(j));
|
||||
// str.append("->");
|
||||
// }
|
||||
// str.append(path.get(path.size() - 1));
|
||||
// path.clear();
|
||||
// }
|
||||
// path.add(String.valueOf(nums[i]));
|
||||
// }
|
||||
// }
|
||||
public List<String> summaryRanges(int[] nums) {
|
||||
|
||||
|
||||
List<String> res = new ArrayList<>();
|
||||
|
||||
int left = 0;
|
||||
int right = 1;
|
||||
while (right < nums.length) {
|
||||
if (nums[right] != nums[right - 1] + 1) {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
stringBuilder.append(nums[left]);
|
||||
if (left != right - 1) {
|
||||
stringBuilder.append("->");
|
||||
stringBuilder.append(nums[right - 1]);
|
||||
}
|
||||
res.add(stringBuilder.toString());
|
||||
left = right;
|
||||
}
|
||||
right++;
|
||||
}
|
||||
if (left < nums.length) {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
stringBuilder.append(nums[left]);
|
||||
if (left != right - 1) {
|
||||
stringBuilder.append("->");
|
||||
stringBuilder.append(nums[right - 1]);
|
||||
}
|
||||
res.add(stringBuilder.toString());
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,102 @@
|
||||
package cn.whaifree.leetCode.Array;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/9/23 11:08
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode289 {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
int[][] board = new int[][]{{0, 1, 0}, {0, 0, 1}, {1, 1, 1}, {0, 0, 0}};
|
||||
new Solution().gameOfLife(board);
|
||||
for (int[] ints : board) {
|
||||
System.out.println(Arrays.toString(ints));
|
||||
}
|
||||
}
|
||||
|
||||
class Solution {
|
||||
|
||||
/**
|
||||
* 为了保证当前修改后的状态不会影响下一个状态的判定,设置另外的状态
|
||||
* 如题所示,只有三种:
|
||||
* 1. 如果当前是活细胞,但是变成了死细胞,那么设置为-1
|
||||
* 2. 如果当前是活细胞,仍然是活细胞,那么不变仍为1
|
||||
* 3. 如果当前是死细胞,但是变成了活细胞,那么设置为2
|
||||
* 那么最后遍历修改完状态之后,将-1修改回为0,2修改回为1
|
||||
* @param board
|
||||
*/
|
||||
public void gameOfLife(int[][] board) {
|
||||
//设置方向来遍历某个节点周围的另外几个节点
|
||||
int[] ner = new int[]{-1,0,1};
|
||||
//获取行和列
|
||||
int rows = board.length;
|
||||
int cols = board[0].length;
|
||||
//遍历每一个节点格子
|
||||
for (int row = 0; row < rows; row++) {
|
||||
for (int col = 0; col < cols; col++) {
|
||||
//设置当前节点周围的存活细胞的数量
|
||||
int liveNer = 0;
|
||||
/**
|
||||
* 当前节点是[ i , j ]
|
||||
* [i-1,j-1] [i-1,j] [i-1,j+1]
|
||||
* [ i ,j-1] [ i ,j] [ i ,j+1]
|
||||
* [i+1,j-1] [i+1,j] [i+1,j+1]
|
||||
* 那么以当前节点为中心,要求周围的节点,则最多是3*3形式
|
||||
* 并且所有的行和列都是用当前节点+1或者-1或者不变构成
|
||||
* 所以我们设置 ner = {-1,0,1} 来形成遍历
|
||||
*/
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (int j = 0; j < 3; j++) {
|
||||
//必须保证不计算当前节点(不计算自己)
|
||||
if (!(ner[i]==0 && ner[j]==0)){
|
||||
//当前节点的相邻节点坐标
|
||||
int r = row+ner[i];
|
||||
int c = col+ner[j];
|
||||
/**判断当前周围节点的存活状态,要求满足两个状态
|
||||
* 1. 必须保证要在 board 矩阵中
|
||||
* 2. 并且**起始状态要是存活,则当前状态为1或者-1都可以(因为这两个状态都表示起始状态为活细胞)**
|
||||
**/
|
||||
if ((r >= 0 && r < rows) && (c >= 0 && c < cols) && (Math.abs(board[r][c]) == 1)) {
|
||||
// -1和1 初始状态都是活细胞
|
||||
liveNer++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/**开始判断当前节点的存活状态
|
||||
* 因为遍历到当前节点的时候,还没有开始修改细胞状态,所以还是0和1的细胞状态
|
||||
* 那么只需要判断状态变化的即可,否则状态不变
|
||||
**/
|
||||
if ((board[row][col]==1) && ( liveNer>3 || liveNer<2)){
|
||||
// -1 代表这个细胞过去是活的现在死了
|
||||
board[row][col]=-1;
|
||||
}
|
||||
if (board[row][col]==0 && ( liveNer==3)){
|
||||
// 2 代表这个细胞过去是死的现在活了
|
||||
board[row][col]=2;
|
||||
}
|
||||
}
|
||||
}
|
||||
//再把额外的状态修改回去
|
||||
for (int row = 0; row < rows; row++) {
|
||||
for (int col = 0; col < cols; col++) {
|
||||
if (board[row][col] == 2) {
|
||||
board[row][col] = 1;
|
||||
}
|
||||
if (board[row][col] == -1){
|
||||
board[row][col] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package cn.whaifree.leetCode.Array;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.PriorityQueue;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/9/22 15:14
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode703 {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
KthLargest kthLargest = new KthLargest(3, new int[]{});
|
||||
System.out.println(kthLargest.add(3));
|
||||
System.out.println(kthLargest.add(5));
|
||||
System.out.println(kthLargest.add(10));
|
||||
System.out.println(kthLargest.add(9));
|
||||
System.out.println(kthLargest.add(4));
|
||||
}
|
||||
|
||||
class KthLargest {
|
||||
|
||||
PriorityQueue<Integer> priorityQueue = null;
|
||||
int size = 0;
|
||||
|
||||
// 小顶堆 第k
|
||||
public KthLargest(int k, int[] nums) {
|
||||
priorityQueue = new PriorityQueue<>();
|
||||
this.size = k;
|
||||
for (int num : nums) {
|
||||
priorityQueue.offer(num);
|
||||
if (priorityQueue.size() > k) {
|
||||
priorityQueue.poll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int add(int val) {
|
||||
priorityQueue.offer(val);
|
||||
if (priorityQueue.size() > size) {
|
||||
priorityQueue.poll();
|
||||
}
|
||||
return priorityQueue.peek();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package cn.whaifree.leetCode.Array;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/9/23 9:18
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode73 {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
int[][] matrix = new int[][]{{1, 1, 1}, {1, 0, 1}, {1, 1, 1}};
|
||||
new Solution().setZeroes(matrix);
|
||||
for (int[] ints : matrix) {
|
||||
System.out.println(Arrays.toString(ints));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class Solution {
|
||||
public void setZeroes(int[][] matrix) {
|
||||
|
||||
int yLen = matrix.length;
|
||||
boolean[] row = new boolean[yLen];
|
||||
int xLen = matrix[0].length;
|
||||
boolean[] col = new boolean[xLen];
|
||||
|
||||
|
||||
for (int y = 0; y < yLen; y++) {
|
||||
for (int x = 0; x < xLen; x++) {
|
||||
if (matrix[y][x] == 0) {
|
||||
row[y] = true;
|
||||
col[x] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int y = 0; y < yLen; y++) {
|
||||
for (int x = 0; x < xLen; x++) {
|
||||
if (row[y] || col[x]) {
|
||||
matrix[y][x] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user