From d3f5af3fd8a951669c110d402047d39658a9ab3f Mon Sep 17 00:00:00 2001 From: eat to 160 pounds <2915390277@qq.com> Date: Mon, 19 Sep 2022 08:57:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=B7=BB=E5=8A=A00063=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E5=86=85=E5=AD=98=E4=BC=98=E5=8C=96javascrip?= =?UTF-8?q?t=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\220\214\350\267\257\345\276\204II.md" | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" index 6b81225cbc..f78995e912 100644 --- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" +++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" @@ -382,8 +382,37 @@ var uniquePathsWithObstacles = function(obstacleGrid) { return dp[m - 1][n - 1] }; + +// 版本二:内存优化,直接以原数组为dp数组 +var uniquePathsWithObstacles = function(obstacleGrid) { + const m = obstacleGrid.length; + const n = obstacleGrid[0].length; + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (obstacleGrid[i][j] === 0) { + // 不是障碍物 + if (i === 0) { + // 取左边的值 + obstacleGrid[i][j] = obstacleGrid[i][j - 1] ?? 1; + } else if (j === 0) { + // 取上边的值 + obstacleGrid[i][j] = obstacleGrid[i - 1]?.[j] ?? 1; + } else { + // 取左边和上边的和 + obstacleGrid[i][j] = obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1]; + } + } else { + // 如果是障碍物,则路径为0 + obstacleGrid[i][j] = 0; + } + } + } + return obstacleGrid[m - 1][n - 1]; +}; ``` + + ### TypeScript ```typescript