Skip to content

Commit b36f8f1

Browse files
committed
Create 63.不同路径 II.js
1 parent 282a143 commit b36f8f1

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

63.不同路径 II.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[][]} obstacleGrid
3+
* @return {number}
4+
*/
5+
var uniquePathsWithObstacles = function(obstacleGrid) {
6+
const dp = [Math.abs(obstacleGrid[0][0] - 1)];
7+
for (let i = 0; i < obstacleGrid.length; i++) {
8+
for (let j = 0; j < obstacleGrid[i].length; j++) {
9+
if (!obstacleGrid[i] || obstacleGrid[i][j] === 1) {
10+
dp[j] = 0;
11+
} else if (!(i === 0 && j === 0)) {
12+
dp[j] = (obstacleGrid[i - 1] && obstacleGrid[i - 1][j] === 0 ? dp[j] : 0) + (obstacleGrid[i][j - 1] === 0 ? dp[j - 1] : 0);
13+
}
14+
}
15+
}
16+
return dp[obstacleGrid[0].length - 1];
17+
};
18+
19+
console.log(uniquePathsWithObstacles([[1]]));

0 commit comments

Comments
 (0)