-
Notifications
You must be signed in to change notification settings - Fork 0
/
62. 不同路径.js
38 lines (37 loc) · 890 Bytes
/
62. 不同路径.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* @param {number} m
* @param {number} n
* @return {number}
*/
// 深度优先遍历 超时
var uniquePaths = function(m, n) {
const dfs = function(i, j, m, n) {
if(i > m || j > n) return 0;
if(i === m || j === n) return 1;
return dfs(i, j + 1, m, n) + dfs(i + 1, j, m, n);
}
return dfs(1, 1, m, n);
};
// 动态规划dp
/**
* @param {number} m
* @param {number} n
* @return {number}
*/
var uniquePaths = function(m, n) {
// 初始化dp[][]
let dp = new Array(m).fill(0).map(() => new Array(n).fill(0))
// 初始化dp[i][0], dp[0][j];
for(let i = 0; i < m; i++) {
dp[i][0] = 1;
}
for(let j = 0; j < n; j++) {
dp[0][j] = 1;
}
for(let i = 1; i < m; i++) {
for(let j = 1; j < n; j++) {
dp[i][j] = dp[i - 1][j] + dp [i][j - 1];
}
}
return dp[m - 1][n - 1];
};