diff --git a/src/main/java/cn/whaifree/designPattern/ChainPattern/ChainPattern.java b/src/main/java/cn/whaifree/designPattern/ChainPattern/ChainPattern.java new file mode 100644 index 0000000..e6b4a15 --- /dev/null +++ b/src/main/java/cn/whaifree/designPattern/ChainPattern/ChainPattern.java @@ -0,0 +1,109 @@ +package cn.whaifree.designPattern.ChainPattern; + +import lombok.Setter; + +import java.util.Scanner; + +/** + * + * ... + * + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/12 23:37 + * @注释 + */ +public class ChainPattern { + + + + + +} + +class Main{ + public static void main(String[] args) { + Audit.Builder builder = new Audit.Builder(); + AbstractAudit build = builder.setAudit(new Supervisor()) + .setAudit(new Manager()) + .setAudit(new Director()) + .build(); + + + Scanner scanner = new Scanner(System.in); + int i = scanner.nextInt(); + for (int j = 0; j < i; j++) { + build.process(scanner.next(), scanner.nextInt()); + } + } + + +} + +interface Audit{ + void process(String name,int day); + + public static class Builder{ + AbstractAudit head; + + public Builder setAudit(AbstractAudit audit) { + if (head == null) { + head = audit; + }else { + AbstractAudit index = head; + index.setNext(audit); + } + return this; + } + + public AbstractAudit build(){ + return head; + } + + } + +} + +@Setter +abstract class AbstractAudit implements Audit{ + Audit next; + +} + +class Supervisor extends AbstractAudit { + + @Override + public void process(String name,int day) { + if (day <= 3) { + System.out.println(name + " Approved by Supervisor"); + } + else { + this.next.process(name, day); + } + } +} + +class Manager extends AbstractAudit { + + @Override + public void process(String name,int day) { + if (day > 3 && day <= 7) { + System.out.println(name + " Approved by Manager"); + }else { + this.next.process(name, day); + } + } +} + +class Director extends AbstractAudit { + + @Override + public void process(String name,int day) { + if (day<=10&&day > 7) { + System.out.println(name + " Approved by Director"); + }else { + System.out.println(name + " Denied by Director."); + } + } +} + diff --git a/src/main/java/cn/whaifree/leetCode/LinkedList/LCR155.java b/src/main/java/cn/whaifree/leetCode/LinkedList/LCR155.java index 5a60f1a..2182ed9 100644 --- a/src/main/java/cn/whaifree/leetCode/LinkedList/LCR155.java +++ b/src/main/java/cn/whaifree/leetCode/LinkedList/LCR155.java @@ -1,16 +1,144 @@ package cn.whaifree.leetCode.LinkedList; -import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +import java.util.*; public class LCR155 { -// https://leetcode.cn/problems/er-cha-sou-suo-shu-yu-shuang-xiang-lian-biao-lcof/description/ + + public static void main(String[] args) { + new Thread(() -> { + List list = new ArrayList(); + while (true) { +// + } + }).start(); + + // 线程二 + new Thread(() -> { + while (true) { + System.out.println(new Date().toString() + Thread.currentThread() + "=="); + try { + Thread.sleep(100); + } catch (Exception e) { + e.printStackTrace(); + } + } + }).start(); + } + + // https://leetcode.cn/problems/er-cha-sou-suo-shu-yu-shuang-xiang-lian-biao-lcof/description/ + static 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; + } + } + + @Test + public void test() { + Node node1 = new Node(4); + node1.left = new Node(2); + node1.right = new Node(5); + node1.left.left = new Node(1); + node1.left.right = new Node(3); + Node node = new Solution1().treeToDoublyList(node1); + + } class Solution { + public Node treeToDoublyList(Node root) { - public TreeNode treeToDoublyList(TreeNode root) { + if (root == null) { + return null; + } + if (root.left == null && root.right == null) { + root.right = root; + root.left = root; + return root; + } + + List nodes = new ArrayList<>(); + Deque deque = new LinkedList<>(); + deque.add(root); + while (!deque.isEmpty()) { + Node pop = deque.pop(); + if (pop != null) { + if (pop.right != null) { + deque.push(pop.right); + } + deque.push(pop); + deque.push(null); + if (pop.left != null) { + deque.push(pop.left); + } + }else { + nodes.add(deque.pop()); + } + } + + + Node head = new Node(Integer.MAX_VALUE); + Node index = head; + for (int i = 0; i < nodes.size() - 1; i++) { + Node node = nodes.get(i); + index.right = node; + node.left = index; + index = index.right; + } + Node node = nodes.get(nodes.size() - 1); + index.right = node; + node.left = index; + node.right = head.right; + head.right.left = node; + + + return head.right; + } + + } + + class Solution1 { + + Node pre; + Node index; + public Node treeToDoublyList(Node root) { + pre = new Node(-1); + index = pre; + 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); } } + + }