-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumPathSum.java
More file actions
20 lines (19 loc) · 807 Bytes
/
Copy pathMinimumPathSum.java
File metadata and controls
20 lines (19 loc) · 807 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class MinimumPathSum {
public int minPathSum(int[][] grid) {
int height = grid.length;
int width = grid[0].length;
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
//(0,0)
if(row == 0 && col == 0) grid[row][col] = grid[row][col];
//第一行
else if(row == 0 && col != 0) grid[row][col] = grid[row][col] + grid[row][col - 1];
//第一列
else if(col == 0 && row != 0) grid[row][col] = grid[row][col] + grid[row - 1][col];
//其他格
else grid[row][col] = grid[row][col] + Math.min(grid[row - 1][col], grid[row][col - 1]);
}
}
return grid[height - 1][width - 1];
}
}