We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 3778cf4 + 89f9573 commit 240684fCopy full SHA for 240684f
problems/0063.不同路径II.md
@@ -550,6 +550,27 @@ function uniquePathsWithObstacles(obstacleGrid: number[][]): number {
550
};
551
```
552
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
574
### Rust
575
576
```Rust
0 commit comments