feat: 2025-10-31打卡
This commit is contained in:
@@ -25,35 +25,32 @@ class Solution {
|
|||||||
if(root == null) {
|
if(root == null) {
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
LinkedList<TreeNode> queue = new LinkedList<>();
|
boolean reverse = false;
|
||||||
ArrayList<TreeNode> stack = new ArrayList<>();
|
|
||||||
queue.add(root);
|
|
||||||
List<List<Integer>> ret = new ArrayList<>();
|
List<List<Integer>> ret = new ArrayList<>();
|
||||||
List<Integer> curLayer = new ArrayList<>();
|
LinkedList<TreeNode> queue = new LinkedList<>();
|
||||||
TreeNode lastLayerNode = null, curLayerNode;
|
queue.add(root);
|
||||||
|
TreeNode layerEndNode = null, curNode = null;
|
||||||
|
List<Integer> cache = new ArrayList<>();
|
||||||
while(!queue.isEmpty()) {
|
while(!queue.isEmpty()) {
|
||||||
TreeNode tmp = queue.pollFirst();
|
TreeNode tmp = queue.pollFirst();
|
||||||
curLayer.add(tmp.val);
|
cache.addLast(tmp.val);
|
||||||
if(tmp.left != null) {
|
if(tmp.left != null) {
|
||||||
// queue.add(tmp.left);
|
queue.addLast(tmp.left);
|
||||||
stack.addLast(tmp.left);
|
curNode = tmp.left;
|
||||||
}
|
}
|
||||||
if(tmp.right != null) {
|
if(tmp.right != null) {
|
||||||
// queue.add(tmp.right);
|
queue.addLast(tmp.right);
|
||||||
stack.addLast(tmp.right);
|
curNode = tmp.right;
|
||||||
}
|
}
|
||||||
if(tmp == lastLayerNode || lastLayerNode == null) {
|
if(layerEndNode == null || layerEndNode == tmp) {
|
||||||
// while()
|
layerEndNode = curNode;
|
||||||
// ret.add(curLayer);
|
if(reverse) {
|
||||||
// curLayer = new ArrayList<>();
|
ret.add(cache.reversed());
|
||||||
// lastLayerNode = curLayerNode;
|
} else {
|
||||||
while(!stack.isEmpty()) {
|
ret.add(cache);
|
||||||
curLayerNode = stack.pollLast();
|
|
||||||
queue.add(curLayerNode);
|
|
||||||
}
|
}
|
||||||
lastLayerNode = curLayerNode;
|
cache = new ArrayList<>();
|
||||||
ret.add(curLayer);
|
reverse = !reverse;
|
||||||
curLayer = new ArrayList<>();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
|
|||||||
31
62.unique-paths.java
Normal file
31
62.unique-paths.java
Normal 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
34
63.unique-paths-ii.java
Normal 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
41
64.minimum-path-sum.java
Normal 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
|
||||||
|
|
||||||
Reference in New Issue
Block a user