File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ public class MaximalSquare {
2+
3+ public int maximalSquare (char [][] matrix ) {
4+ if (matrix == null || matrix .length < 1 || matrix [0 ].length < 1 ) {
5+ return 0 ;
6+ }
7+
8+ int height = matrix .length ;
9+ int width = matrix [0 ].length ;
10+ int maxSide = 0 ;
11+
12+ // 相当于已经预处理新增第一行、第一列均为0
13+ int [][] dp = new int [height + 1 ][width + 1 ];
14+
15+ for (int row = 0 ; row < height ; row ++) {
16+ for (int col = 0 ; col < width ; col ++) {
17+ if (chekIsOne (matrix [row ][col ])) {
18+ dp [row + 1 ][col + 1 ] = Math .min (Math .min (dp [row + 1 ][col ], dp [row ][col + 1 ]), dp [row ][col ]) + 1 ;
19+ maxSide = Math .max (maxSide , dp [row + 1 ][col + 1 ]);
20+ }
21+ }
22+ }
23+ return maxSide * maxSide ;
24+ }
25+
26+ private boolean chekIsOne (char ch ) {
27+ return Character .getNumericValue (ch ) == 1 ;
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ public class MinPathSum {
2+
3+ public int minPathSum (int [][] grid ) {
4+ for (int i = grid .length - 1 ; i >= 0 ; i --) {
5+ for (int j = grid [0 ].length - 1 ; j >= 0 ; j --) {
6+ if (i == grid .length - 1 && j != grid [0 ].length - 1 ) {
7+ grid [i ][j ] = grid [i ][j ] + grid [i ][j + 1 ];
8+ } else if (j == grid [0 ].length - 1 && i != grid .length - 1 ) {
9+ grid [i ][j ] = grid [i ][j ] + grid [i + 1 ][j ];
10+ } else if (j != grid [0 ].length - 1 && i != grid .length - 1 ) {
11+ grid [i ][j ] = grid [i ][j ] + Math .min (grid [i + 1 ][j ], grid [i ][j + 1 ]);
12+ } else {
13+ grid [i ][j ] = grid [i ][j ];
14+ }
15+ }
16+ }
17+ return grid [0 ][0 ];
18+ }
19+ }
Original file line number Diff line number Diff line change 1- 学习笔记
1+ 学习笔记
2+
3+ 算法到后面越来越难了,大学没学高数吃亏很多,看来要安排时间去补下高数的课了。
You can’t perform that action at this time.
0 commit comments