feat: 2025-10-31打卡

This commit is contained in:
wu xiangkai
2025-10-31 10:00:38 +08:00
parent 71664a10cc
commit 5a9ae36d6b
4 changed files with 124 additions and 21 deletions

View File

@@ -25,35 +25,32 @@ class Solution {
if(root == null) {
return new ArrayList<>();
}
LinkedList<TreeNode> queue = new LinkedList<>();
ArrayList<TreeNode> stack = new ArrayList<>();
queue.add(root);
boolean reverse = false;
List<List<Integer>> ret = new ArrayList<>();
List<Integer> curLayer = new ArrayList<>();
TreeNode lastLayerNode = null, curLayerNode;
LinkedList<TreeNode> queue = new LinkedList<>();
queue.add(root);
TreeNode layerEndNode = null, curNode = null;
List<Integer> cache = new ArrayList<>();
while(!queue.isEmpty()) {
TreeNode tmp = queue.pollFirst();
curLayer.add(tmp.val);
cache.addLast(tmp.val);
if(tmp.left != null) {
// queue.add(tmp.left);
stack.addLast(tmp.left);
queue.addLast(tmp.left);
curNode = tmp.left;
}
if(tmp.right != null) {
// queue.add(tmp.right);
stack.addLast(tmp.right);
queue.addLast(tmp.right);
curNode = tmp.right;
}
if(tmp == lastLayerNode || lastLayerNode == null) {
// while()
// ret.add(curLayer);
// curLayer = new ArrayList<>();
// lastLayerNode = curLayerNode;
while(!stack.isEmpty()) {
curLayerNode = stack.pollLast();
queue.add(curLayerNode);
if(layerEndNode == null || layerEndNode == tmp) {
layerEndNode = curNode;
if(reverse) {
ret.add(cache.reversed());
} else {
ret.add(cache);
}
lastLayerNode = curLayerNode;
ret.add(curLayer);
curLayer = new ArrayList<>();
cache = new ArrayList<>();
reverse = !reverse;
}
}
return ret;

31
62.unique-paths.java Normal file
View File

@@ -0,0 +1,31 @@
/*
* @lc app=leetcode id=62 lang=java
*
* [62] Unique Paths
*/
// @lc code=start
class Solution {
public int uniquePaths(int m, int n) {
int[][] dp = new int[m][n];
for(int i = 0; i<dp.length; i++) {
for(int j=0; j<dp[0].length; j++) {
dp[i][j] = 0;
}
}
dp[0][0] = 1;
for(int i = 0; i<dp.length; i++) {
for(int j=0; j<dp[0].length; j++) {
if(i-1>=0) {
dp[i][j] += dp[i-1][j];
}
if(j-1 >=0) {
dp[i][j] += dp[i][j-1];
}
}
}
return dp[m-1][n-1];
}
}
// @lc code=end

34
63.unique-paths-ii.java Normal file
View File

@@ -0,0 +1,34 @@
/*
* @lc app=leetcode id=63 lang=java
*
* [63] Unique Paths II
*/
// @lc code=start
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int[][] dp = new int[obstacleGrid.length][obstacleGrid[0].length];
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
dp[i][j] = 0;
}
}
dp[0][0] = obstacleGrid[0][0]==1?0:1;
for(int i = 0; i<dp.length; i++) {
for(int j=0; j<dp[0].length; j++) {
if(obstacleGrid[i][j] == 1) {
continue;
}
if(i-1>=0) {
dp[i][j] += dp[i-1][j];
}
if(j-1>=0) {
dp[i][j] += dp[i][j-1];
}
}
}
return dp[dp.length-1][dp[0].length-1];
}
}
// @lc code=end

41
64.minimum-path-sum.java Normal file
View File

@@ -0,0 +1,41 @@
/*
* @lc app=leetcode id=64 lang=java
*
* [64] Minimum Path Sum
*/
// @lc code=start
class Solution {
public int minPathSum(int[][] grid) {
int[][] dp = new int[grid.length][grid[0].length];
for(int i=0; i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
dp[i][j] = -1;
}
}
dp[0][0] = grid[0][0];
for(int i = 0; i<dp.length; i++) {
for(int j=0; j < dp[0].length; j++) {
if(i-1>=0) {
// update from upside
if(dp[i][j] < 0) {
dp[i][j] = dp[i-1][j] + grid[i][j];
} else {
dp[i][j] = Math.min(dp[i][j], dp[i-1][j] + grid[i][j]);
}
}
if(j-1>=0) {
// update from leftside
if(dp[i][j] < 0) {
dp[i][j] = dp[i][j-1] + grid[i][j];
} else {
dp[i][j] = Math.min(dp[i][j], dp[i][j-1] + grid[i][j]);
}
}
}
}
return dp[dp.length-1][dp[0].length-1];
}
}
// @lc code=end