From 5a9ae36d6b349bf733084129b1bc38a4aa70d727 Mon Sep 17 00:00:00 2001 From: wu xiangkai Date: Fri, 31 Oct 2025 10:00:38 +0800 Subject: [PATCH] =?UTF-8?q?feat:=202025-10-31=E6=89=93=E5=8D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ary-tree-zigzag-level-order-traversal.java | 39 ++++++++---------- 62.unique-paths.java | 31 ++++++++++++++ 63.unique-paths-ii.java | 34 +++++++++++++++ 64.minimum-path-sum.java | 41 +++++++++++++++++++ 4 files changed, 124 insertions(+), 21 deletions(-) create mode 100644 62.unique-paths.java create mode 100644 63.unique-paths-ii.java create mode 100644 64.minimum-path-sum.java diff --git a/103.binary-tree-zigzag-level-order-traversal.java b/103.binary-tree-zigzag-level-order-traversal.java index aef2570..1834168 100644 --- a/103.binary-tree-zigzag-level-order-traversal.java +++ b/103.binary-tree-zigzag-level-order-traversal.java @@ -25,35 +25,32 @@ class Solution { if(root == null) { return new ArrayList<>(); } - LinkedList queue = new LinkedList<>(); - ArrayList stack = new ArrayList<>(); - queue.add(root); + boolean reverse = false; List> ret = new ArrayList<>(); - List curLayer = new ArrayList<>(); - TreeNode lastLayerNode = null, curLayerNode; + LinkedList queue = new LinkedList<>(); + queue.add(root); + TreeNode layerEndNode = null, curNode = null; + List 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; diff --git a/62.unique-paths.java b/62.unique-paths.java new file mode 100644 index 0000000..20411c6 --- /dev/null +++ b/62.unique-paths.java @@ -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=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 + diff --git a/63.unique-paths-ii.java b/63.unique-paths-ii.java new file mode 100644 index 0000000..4b48f56 --- /dev/null +++ b/63.unique-paths-ii.java @@ -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=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 + diff --git a/64.minimum-path-sum.java b/64.minimum-path-sum.java new file mode 100644 index 0000000..aafdec0 --- /dev/null +++ b/64.minimum-path-sum.java @@ -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=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 +