62.不同路径
class Solution {
public int uniquePaths(int m, int n) {
// int[] rows = new int[n];
// int[] cols = new int[n];
// Arrays.fill(rows, 1);
// Arrays.fill(cols, 1);
// for(int i = 1; i < m; i++) {
// for(int j = 1; j < n; j++) {
// cols[j] = cols[j - 1] + rows[j];
// }
// rows = cols.clone();
// }
// return rows[n - 1];
int[][] arr = new int[m][n];
for(int i = 0; i < n; i++) arr[0][i] = 1;
for(int i = 0; i < m; i++) arr[i][0] = 1;
for(int i = 1; i < m; i++) {
for(int j = 1; j < n; j++) {
arr[i][j] = arr[i - 1][j] + arr[i][j - 1];
}
}
return arr[m - 1][n - 1];
}
}
class Solution {
public int uniquePaths(int m, int n) {
int[] arr = new int[n];
Arrays.fill(arr, 1);
for(int i = 1; i < m; i++) {
for(int j = 1; j < n; j++) {
arr[j] += arr[j - 1];
}
}
return arr[n - 1];
}
}
63.不同路径2
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length, n = obstacleGrid[0].length;
int[][] dp = new int[m][n];
for(int i = 0; i < m && obstacleGrid[i][0] == 0; i++) {
dp[i][0] = 1;
}
for(int i = 0; i < n && obstacleGrid[0][i] == 0; i++) {
dp[0][i] = 1;
}
for(int i = 1; i < m; i++) {
for(int j = 1; j < n; j++) {
if(obstacleGrid[i][j] == 0) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
}
return dp[m - 1][n - 1];
}
}
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length, n = obstacleGrid[0].length;
int[] arr = new int[n + 1];
arr[1] = 1;
for(int i = 0; i < m; i++) {
for(int j = 1; j <= n; j++) {
if(obstacleGrid[i][j - 1] != 1) {
arr[j] += arr[j - 1];
}
else {
arr[j] = 0;
}
}
}
return arr[n];
}
}