初始化(0,0)
class Solution { public: int uniquePaths(int m, int n) { vector<vector<int>> result(m,vector<int>(n)); result[0][0] = 1; for(int i = 0;i < m;i++){ for(int j = 0;j < n;j++){ if(i == 0 && j == 0) continue; if(i != 0 && j != 0){ result[i][j] = result[i][j-1] + result[i-1][j]; } else if(i == 0) result[i][j] = result[i][j-1]; else result[i][j] = result[i-1][j]; } } return result[m-1][n-1]; } };
初始化第一行第一列
class Solution { public: int uniquePaths(int m, int n) { vector<vector<int>> result(m,vector<int>(n)); result[0][0] = 1; for(int i = 1;i < m;i++) result[i][0] = 1; for(int j = 1;j < n;j++) result[0][j] = 1; for(int i = 1;i < m;i++){ for(int j = 1;j < n;j++){ result[i][j] = result[i-1][j] + result[i][j-1]; } } return result[m-1][n-1]; } };