Maximal Square(最大正方形)

http://www.lintcode.com/en/problem/maximal-square/?rand=true

public class Solution {
    /*
     * @param matrix: a matrix of 0 and 1
     * @return: an integer
     */
    public int maxSquare(int[][] matrix) {
        // write your code
        int res = 0;
//        matrix修改为当前表示的正方形边长
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                if (matrix[i][j] > 0) {
                    if (i > 0 && j > 0) {
                        matrix[i][j] = Math.min(matrix[i - 1][j], Math.min(matrix[i - 1][j - 1], matrix[i][j - 1])) + 1;
                    }
                    res = Math.max(res, matrix[i][j]);
                }
            }
        }
        return res * res;
    }
}

你可能感兴趣的:(Maximal Square(最大正方形))