Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d3e1f1d

Browse files
添加(0063.不同路径II.md):增加typescript版本
1 parent f409384 commit d3e1f1d

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

‎problems/0063.不同路径II.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,38 @@ var uniquePathsWithObstacles = function(obstacleGrid) {
352352
};
353353
```
354354

355-
C
355+
### TypeScript
356+
357+
```typescript
358+
function uniquePathsWithObstacles(obstacleGrid: number[][]): number {
359+
/**
360+
dp[i][j]: 到达(i, j)的路径数
361+
dp[0][*]: 用u表示第一个障碍物下标,则u之前为1,u之后(含u)为0
362+
dp[*][0]: 同上
363+
...
364+
dp[i][j]: obstacleGrid[i][j] === 1 ? 0 : dp[i-1][j] + dp[i][j-1];
365+
*/
366+
const m: number = obstacleGrid.length;
367+
const n: number = obstacleGrid[0].length;
368+
const dp: number[][] = new Array(m).fill(0).map(_ => new Array(n).fill(0));
369+
for (let i = 0; i < m && obstacleGrid[i][0] === 0; i++) {
370+
dp[i][0] = 1;
371+
}
372+
for (let i = 0; i < n && obstacleGrid[0][i] === 0; i++) {
373+
dp[0][i] = 1;
374+
}
375+
for (let i = 1; i < m; i++) {
376+
for (let j = 1; j < n; j++) {
377+
if (obstacleGrid[i][j] === 1) continue;
378+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
379+
}
380+
}
381+
return dp[m - 1][n - 1];
382+
};
383+
```
384+
385+
### C
386+
356387
```c
357388
//初始化dp数组
358389
int **initDP(int m, int n, int** obstacleGrid) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /