Skip to content

Commit

Permalink
同步
Browse files Browse the repository at this point in the history
  • Loading branch information
r6hk committed Dec 4, 2024
1 parent d085aff commit 57cf3ce
Show file tree
Hide file tree
Showing 13 changed files with 344 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,4 @@ fabric.properties
*/target/
/.fastRequest/
/.idea/
/.xcodemap/
32 changes: 32 additions & 0 deletions algorithm/src/main/java/dev/rennen/leetcode/Test209.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}));
}
}
82 changes: 82 additions & 0 deletions algorithm/src/main/java/dev/rennen/leetcode/Test427.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
33 changes: 33 additions & 0 deletions algorithm/src/main/java/dev/rennen/leetcode/Test67.java
Original file line number Diff line number Diff line change
@@ -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"));
}

}
63 changes: 63 additions & 0 deletions algorithm/src/main/java/dev/rennen/template/UnionFind.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
33 changes: 33 additions & 0 deletions basic-jdk17/src/main/java/dev/rennen/ExceptionTest.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
74 changes: 74 additions & 0 deletions basic-jdk17/src/main/java/dev/rennen/TryCatchTest.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
6 changes: 6 additions & 0 deletions spring-project/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@
<version>5.3.18</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
</dependency>
</dependencies>


Expand Down
4 changes: 4 additions & 0 deletions spring-project/src/main/java/dev/rennen/Product.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
7 changes: 7 additions & 0 deletions spring-project/src/main/java/dev/rennen/ProductFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.rennen;

public class ProductFactory {
public Product createProduct(String name, int price) {
return new Product();
}
}
8 changes: 7 additions & 1 deletion spring-project/src/main/resources/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<bean id="product" class="dev.rennen.Product"/>
<bean id="productFactory" class="dev.rennen.ProductFactory"/>

<bean id="myProduct" factory-bean="productFactory" factory-method="createProduct">
<constructor-arg value="Sample Product"/>
<constructor-arg value="100"/>
</bean>

</beans>
2 changes: 0 additions & 2 deletions spring-project/src/test/java/dev/rennen/FactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,5 @@ public class FactoryTest {

@Test
public void XmlFactoryTest() {
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("config.xml"));
beanFactory.getBean("product");
}
}
Loading

0 comments on commit 57cf3ce

Please sign in to comment.