Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions problems/0063.不同路径II.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down