連結:https://leetcode.com/problems/unique-paths/description/
class Solution {
public int uniquePaths(int m, int n) {
int[][] dp = new int[m][n];
for (int i = 0; i < m; i++) {
dp[i][0] = 1;
}
for (int j = 0; j < n; j++) {
dp[0][j] = 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-1][n-1];
}
}
連結:https://leetcode.com/problems/unique-paths-ii/description/
class Solution {
public int uniquePathsWithObstacles(int[][] A) {
int n = A.length;
int m = A[0].length;
int [][] ans = new int[n][m];
for(int i = 0;i<n;i++){
if(A[i][0] == 0) ans[i][0] = 1;
else break;
}
for(int j = 0;j<m;j++){
if(A[0][j] == 0) ans[0][j] = 1;
else break;
}
for(int i = 1;i<n;i++){
for(int j = 1;j<m;j++){
if(A[i][j]==0) ans[i][j] = ans[i-1][j]+ans[i][j-1];
}
}
return ans[n-1][m-1];
}
}