-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
344 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,3 +104,4 @@ fabric.properties | |
*/target/ | ||
/.fastRequest/ | ||
/.idea/ | ||
/.xcodemap/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
63
algorithm/src/main/java/dev/rennen/template/UnionFind.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.