码迷,mamicode.com
首页 > 其他好文 > 详细

[LC] 221. Maximal Square

时间:2019-12-27 13:47:34      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:example   led   ini   ||   and   length   col   put   int   

Given a 2D binary matrix filled with 0‘s and 1‘s, find the largest square containing only 1‘s and return its area.

Example:

Input: 

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0

Output: 4

class Solution {
    public int maximalSquare(char[][] matrix) {
        if (matrix == null || matrix.length == 0) {
            return 0;
        }
        
        int row = matrix.length;
        int col = matrix[0].length;
        int[][] sqr = new int[row + 1][col + 1];
        int res = 0;

        for (int i = 1; i <= row; i++) {
            for(int j = 1; j <= col; j++) {
                if (matrix[i - 1][j - 1] == ‘1‘) {
                                    sqr[i][j] = Math.min(Math.min(sqr[i - 1][j], sqr[i][j - 1]), sqr[i - 1][j - 1]) + 1;
                res = Math.max(res, sqr[i][j]);
                }
            }
        }
        return res * res;
    }
}

[LC] 221. Maximal Square

标签:example   led   ini   ||   and   length   col   put   int   

原文地址:https://www.cnblogs.com/xuanlu/p/12106634.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!