此题涉及到动态规划,不过题目比较简单,在二维数组中计算经过每个点的路径的总和,通过dp数组来记录每个点,不断往下迭代即可;
代码如下:
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int>> dp;
vector<int> y;
for(int i = 0;i <= n;i++){
y.push_back(0);
}
dp.push_back(y);
y.clear();
for(int i = 0;i < m;i++){
for(int j = 0;j <= n;j++){
if(j == 0){
y.push_back(0);
}else{
y.push_back(1);
}
}
dp.push_back(y);
y.clear();
}
dp[1][0] = 1;
for(int i = 1;i <= m;i++){
for(int j = 1;j <= n;j++){
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
return dp[m][n];
}
};