- 不同路径
class Solution {
public int uniquePaths(int m, int n) {
int row = m + 1;
int colum = n + 1;
int[][] dp = new int[row][colum];
// 初始dp数组第一行和第一列
for (int i = 0; i < colum; i++) {
dp[0][i] = 1;
}
for (int i = 1; i < row; i++) {
dp[i][0] = 1;
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
// System.out.println("row" + ""+ i +"colum" +j);
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
// System.out.print(dp[i][j] + " ");
}
// System.out.println();
}
return dp[m-1][n-1];
}
}
注意事项
遍历数组的边界要注意,m个元素边界到m-1。初始化数组new 时用的时m