diff --git a/.gitignore b/.gitignore index 4f4ebc6..00b7882 100644 --- a/.gitignore +++ b/.gitignore @@ -104,3 +104,4 @@ fabric.properties */target/ /.fastRequest/ /.idea/ +/.xcodemap/ diff --git a/algorithm/src/main/java/dev/rennen/leetcode/Test209.java b/algorithm/src/main/java/dev/rennen/leetcode/Test209.java index c4f3cf0..8b198ce 100644 --- a/algorithm/src/main/java/dev/rennen/leetcode/Test209.java +++ b/algorithm/src/main/java/dev/rennen/leetcode/Test209.java @@ -5,4 +5,36 @@ * @date 2024/10/26 16:16 */ public class Test209 { + + public static int minSubArrayLen(int target, int[] nums) { + // 前缀和二分 + int n = nums.length; + int[] preSum = new int[n + 1]; + for (int i = 1; i < n + 1; i++) { + preSum[i] = preSum[i - 1] + nums[i - 1]; + } + int res = 0x3f3f3f; + for (int i = 0; i < n + 1; i++) { + int t = preSum[i]; + int j = binarySearch(preSum, t + target); + if (j >= 0 && j <= n) res = Math.min(res, j - i); + } + return res == 0x3f3f3f ? 0 : res; + } + + // 0 2 5 6 8 12 15 + private static int binarySearch(int[] nums, int target) { + int l = 0; + int r = nums.length - 1; + while (l <= r) { + int m = l + (r - l) / 2; + if (nums[m] >= target) r = m - 1; + else l = m + 1; + } + return l; + } + + public static void main(String[] args) { + System.out.println(minSubArrayLen(4, new int[]{1, 4, 4})); + } } diff --git a/algorithm/src/main/java/dev/rennen/leetcode/Test427.java b/algorithm/src/main/java/dev/rennen/leetcode/Test427.java new file mode 100644 index 0000000..5af62ad --- /dev/null +++ b/algorithm/src/main/java/dev/rennen/leetcode/Test427.java @@ -0,0 +1,82 @@ +package dev.rennen.leetcode; + +/** + * @author rennen.dev + * @date 2024/11/3 16:49 + */ +public class Test427 { + + public Node construct(int[][] grid) { + return construct(grid, 0, 0, grid.length - 1, grid.length - 1); + } + + private Node construct(int[][] grid, int ax, int ay, int bx, int by) { + boolean val = grid[ax][ay] == 1; + if (checkSame(grid, ax, ay, bx, by)) return new Node(val, true); + else { + int midx = ax + (bx - ax) / 2; + int midy = ay + (by - ay) / 2; + return new Node(val, + false, + construct(grid, ax, ay, midx, midy), + construct(grid, ax, midy + 1, midx, by), + construct(grid, midx + 1, ay, bx, midy), + construct(grid, midx + 1, midy + 1, bx, by)); + } + } + + private boolean checkSame(int[][] grid, int ax, int ay, int bx, int by) { + int first = grid[ax][ay]; + for (int i = ax; i <= bx; i++) { + for (int j = ay; j <= by; j++) { + if (grid[i][j] != first) return false; + } + } + return true; + } + + public static void main(String[] args) { + int[][] grid = {{1,1,1,1,0,0,0,0},{1,1,1,1,0,0,0,0},{1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1},{1,1,1,1,0,0,0,0},{1,1,1,1,0,0,0,0},{1,1,1,1,0,0,0,0},{1,1,1,1,0,0,0,0}}; + Test427 solution = new Test427(); + Node result = solution.construct(grid); + System.out.println(result); + } + +} + +class Node { + public boolean val; + public boolean isLeaf; + public Node topLeft; + public Node topRight; + public Node bottomLeft; + public Node bottomRight; + + + public Node() { + this.val = false; + this.isLeaf = false; + this.topLeft = null; + this.topRight = null; + this.bottomLeft = null; + this.bottomRight = null; + } + + public Node(boolean val, boolean isLeaf) { + this.val = val; + this.isLeaf = isLeaf; + this.topLeft = null; + this.topRight = null; + this.bottomLeft = null; + this.bottomRight = null; + } + + public Node(boolean val, boolean isLeaf, Node topLeft, Node topRight, Node bottomLeft, Node bottomRight) { + this.val = val; + this.isLeaf = isLeaf; + this.topLeft = topLeft; + this.topRight = topRight; + this.bottomLeft = bottomLeft; + this.bottomRight = bottomRight; + } +} diff --git a/algorithm/src/main/java/dev/rennen/leetcode/Test67.java b/algorithm/src/main/java/dev/rennen/leetcode/Test67.java new file mode 100644 index 0000000..c440132 --- /dev/null +++ b/algorithm/src/main/java/dev/rennen/leetcode/Test67.java @@ -0,0 +1,33 @@ +package dev.rennen.leetcode; + +/** + * @author rennen.dev + * @date 2024/12/2 9:22 + */ +public class Test67 { + + public static String addBinary(String a, String b) { + // 将二进制字符串转换为整数 + int x = Integer.parseInt(a, 2); + int y = Integer.parseInt(b, 2); + + // 进行二进制加法 + while (y != 0) { + // 计算无进位的和 + int answer = x ^ y; // 0001 + // 计算进位部分 + int carry = (x & y) << 1; + // 更新 x 和 y + x = answer; + y = carry; + } + + // 将结果转换回二进制字符串并去掉"0b"前缀 + return Integer.toBinaryString(x); + } + + public static void main(String[] args) { + System.out.println(addBinary("1010", "1011")); + } + +} diff --git a/algorithm/src/main/java/dev/rennen/template/UnionFind.java b/algorithm/src/main/java/dev/rennen/template/UnionFind.java new file mode 100644 index 0000000..dbde13e --- /dev/null +++ b/algorithm/src/main/java/dev/rennen/template/UnionFind.java @@ -0,0 +1,63 @@ +package dev.rennen.template; + +/** + * @author rennen.dev + * @date 2024/10/27 20:30 + */ +public class UnionFind { + + /** + * 初始化并查集 + * @param parent 一个数组,用来表示并查集 + */ + private static void init(int[] parent) { + int n = parent.length; + for (int i = 0; i < n; i++) { + parent[i] = i; + } + } + + /** + * 将元素 a 和 元素 b 所在的集合合并成一个集合 + * @param a 元素 a 序号 + * @param b 元素 b 序号 + * @param parent 并查集数组 + */ + private static void union(int a, int b, int[] parent) { + int rootA = find(a, parent); + int rootB = find(b, parent); + if (rootA != rootB) { + // 下面的写法是为了之后迭代查找祖先时「引用链」更短,可避免超时 + if (rootA < rootB) parent[rootB] = rootA; + else parent[rootA] = rootB; + } + } + + /** + * 查询元素 x 的祖先 + * @param x 元素 x 序号 + * @param parent 并查集数组 + * @return 元素 x 祖先的序号 + */ + private static int find(int x, int[] parent) { + while (parent[x] != x) { + x = parent[x]; + } + return x; + } + + /** + * 查找当前并查集一共有多少个集合 + * @param parent 并查集集合 + * @return 当前并查集一共有多少个集合 + */ + private static int unionCount(int[] parent) { + int n = parent.length, count = 0; + for (int i = 0; i < n; i++) { + if (parent[i] != i) { + count++; + } + } + return count; + } +} diff --git a/basic-jdk17/src/main/java/dev/rennen/ExceptionTest.java b/basic-jdk17/src/main/java/dev/rennen/ExceptionTest.java new file mode 100644 index 0000000..cca394b --- /dev/null +++ b/basic-jdk17/src/main/java/dev/rennen/ExceptionTest.java @@ -0,0 +1,33 @@ +package dev.rennen; + +import java.io.IOException; + +/** + * @author rennen.dev + * @date 2024/12/3 11:58 + */ +public class ExceptionTest { + + public static void main(String[] args) { + int[] nums = new int[]{1, 2, 3, 4, 5}; + for (int num : nums) { + num = 0; + } + for (int num : nums) { + System.out.println(num); + } + + // test(); + } + + private static void test() { + try { + System.out.println("try"); + throw new IOException(); + } catch (Exception e) { + System.out.println("catch"); + } finally { + System.out.println("finally"); + } + } +} diff --git a/basic-jdk17/src/main/java/dev/rennen/TryCatchTest.java b/basic-jdk17/src/main/java/dev/rennen/TryCatchTest.java new file mode 100644 index 0000000..cf871e2 --- /dev/null +++ b/basic-jdk17/src/main/java/dev/rennen/TryCatchTest.java @@ -0,0 +1,74 @@ +package dev.rennen; + +/** + * @author rennen.dev + * @date 2024/12/1 10:50 + */ +public class TryCatchTest { + + public int vaule = 0; + + public static void main(String[] args) { +// System.out.println("test1()函数返回:" + test1()); +// System.out.println("test2()函数返回:" + test2().vaule); + System.out.println("test3()函数返回:" + test3()); + System.out.println("test4()函数返回:" + test4()); + } + + private static int test1(){ + int i = 0; + try { + System.out.println("Try block executing: " + ++i); + return i; + }catch (Exception e){ + System.out.println("Catch Error executing: " + ++i); + return -1; + }finally { + System.out.println("finally executing: " + ++i); + } + } + + private static TryCatchTest test2(){ + TryCatchTest t = new TryCatchTest(); + try { + t.vaule = 1; + System.out.println("Try block executing: " + t.vaule); + return t; + }catch (Exception e){ + t.vaule = -1; + System.out.println("Catch Error executing: " + t.vaule); + return t; + }finally { + t.vaule = 3; + System.out.println("finally executing: " + t.vaule); + } + } + + private static int test3(){ + int i = 0; + try { + System.out.println("Try block executing: " + ++i); + return ++i; + }catch (Exception e){ + System.out.println("Catch Error executing: " + ++i); + return -1; + }finally { + System.out.println("finally executing: " + ++i); + return i; + } + } + + private static int test4(){ + int i = 0; + try { + System.out.println("Try block executing: " + ++i); + throw new Exception(); + }catch (Exception e){ + System.out.println("Catch Error executing: " + ++i); + return -1; + }finally { + System.out.println("finally executing: " + ++i); + return i; + } + } +} diff --git a/spring-project/pom.xml b/spring-project/pom.xml index f281b89..f1e4f2a 100644 --- a/spring-project/pom.xml +++ b/spring-project/pom.xml @@ -93,6 +93,12 @@ 5.3.18 test + + + org.projectlombok + lombok + 1.18.34 + diff --git a/spring-project/src/main/java/dev/rennen/Product.java b/spring-project/src/main/java/dev/rennen/Product.java index 6f327ac..f9b5347 100644 --- a/spring-project/src/main/java/dev/rennen/Product.java +++ b/spring-project/src/main/java/dev/rennen/Product.java @@ -1,9 +1,13 @@ package dev.rennen; +import lombok.AllArgsConstructor; +import lombok.Data; + /** * @author rennen.dev * @date 2024/10/24 19:31 */ +@Data public class Product { private String name; diff --git a/spring-project/src/main/java/dev/rennen/ProductFactory.java b/spring-project/src/main/java/dev/rennen/ProductFactory.java new file mode 100644 index 0000000..d22dab3 --- /dev/null +++ b/spring-project/src/main/java/dev/rennen/ProductFactory.java @@ -0,0 +1,7 @@ +package dev.rennen; + +public class ProductFactory { + public Product createProduct(String name, int price) { + return new Product(); + } +} diff --git a/spring-project/src/main/resources/config.xml b/spring-project/src/main/resources/config.xml index aee1023..13299fb 100644 --- a/spring-project/src/main/resources/config.xml +++ b/spring-project/src/main/resources/config.xml @@ -3,5 +3,11 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> - + + + + + + + \ No newline at end of file diff --git a/spring-project/src/test/java/dev/rennen/FactoryTest.java b/spring-project/src/test/java/dev/rennen/FactoryTest.java index c9e696c..cec1bea 100644 --- a/spring-project/src/test/java/dev/rennen/FactoryTest.java +++ b/spring-project/src/test/java/dev/rennen/FactoryTest.java @@ -13,7 +13,5 @@ public class FactoryTest { @Test public void XmlFactoryTest() { - BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("config.xml")); - beanFactory.getBean("product"); } } diff --git a/springboot-project/src/main/java/dev/rennen/springbootproject/SpringbootProjectApplication.java b/springboot-project/src/main/java/dev/rennen/springbootproject/SpringbootProjectApplication.java index 0a07743..c17ca99 100644 --- a/springboot-project/src/main/java/dev/rennen/springbootproject/SpringbootProjectApplication.java +++ b/springboot-project/src/main/java/dev/rennen/springbootproject/SpringbootProjectApplication.java @@ -3,13 +3,14 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class SpringbootProjectApplication { public static void main(String[] args) { - SpringApplication.run(SpringbootProjectApplication.class, args); + ConfigurableApplicationContext context = SpringApplication.run(SpringbootProjectApplication.class, args); } }