Skip to content

Commit 240684f

Browse files
Merge pull request youngyangyang04#2571 from xCk27x/master
新增 0063.不同路径II 的TypeScript作法2
2 parents 3778cf4 + 89f9573 commit 240684f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

problems/0063.不同路径II.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,27 @@ function uniquePathsWithObstacles(obstacleGrid: number[][]): number {
550550
};
551551
```
552552
553+
// 版本二: dp改為使用一維陣列,從終點開始遍歷
554+
```typescript
555+
function uniquePathsWithObstacles(obstacleGrid: number[][]): number {
556+
const m = obstacleGrid.length;
557+
const n = obstacleGrid[0].length;
558+
559+
const dp: number[] = new Array(n).fill(0);
560+
dp[n - 1] = 1;
561+
562+
// 由下而上,右而左進行遍歷
563+
for (let i = m - 1; i >= 0; i--) {
564+
for (let j = n - 1; j >= 0; j--) {
565+
if (obstacleGrid[i][j] === 1) dp[j] = 0;
566+
else dp[j] = dp[j] + (dp[j + 1] || 0);
567+
}
568+
}
569+
570+
return dp[0];
571+
};
572+
```
573+
553574
### Rust
554575
555576
```Rust

0 commit comments

Comments
 (0)