# LeetCode Maximal Square

Description:

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

For example, given the following matrix:

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

Solution:

import java.util.*;

public class Solution {
public int maximalSquare(char[][] matrix) {
int m = matrix.length;
if (m == 0)
return 0;
int n = matrix[0].length;
int len = Math.min(m, n);

loop: for (int l = 1; l <= len; l++) {
for (int i = 0; i <= m - l; i++) {
for (int j = 0; j <= n - l; j++) {
if (matrix[i][j] == '1' && valid(matrix, i, j, l))
continue loop;
}
}
return (l - 1) * (l - 1);
}
return len * len;
}

boolean valid(char[][] matrix, int a, int b, int len) {
for (int i = a; i < a + len; i++)
for (int j = b; j < b + len; j++)
if (matrix[i][j] != '1')
return false;
return true;
}
}

import java.util.*;

public class Solution {
public int maximalSquare(char[][] matrix) {
int m = matrix.length;
if (m == 0)
return 0;
int n = matrix[0].length;
int dp[][] = new int[m][n];
int max = 0;

for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (matrix[i][j] == '1') {
dp[i][j] = 1;
max = 1;
}

int maxL = Math.max(m, n);
for (int l = 1; l < maxL; l++) {
for (int i = 0; i < m - l; i++)
for (int j = 0; j < n - l; j++) {
if (dp[i][j] == l && dp[i][j + 1] == l && dp[i + 1][j] == l
&& dp[i + 1][j + 1] == l) {
dp[i][j] = l + 1;
max = dp[i][j];
}
}
}

return max * max;
}
}

import java.util.*;

public class Solution {
public int maximalSquare(char[][] matrix) {
int m = matrix.length;
if (m == 0)
return 0;
int n = matrix[0].length;
int dp[][] = new int[m][n];
int max = 0;

for (int i = 0; i < m; i++)
if (matrix[i][n - 1] == '1') {
dp[i][n - 1] = 1;
max = 1;
}
for (int j = 0; j < n; j++)
if (matrix[m - 1][j] == '1') {
dp[m - 1][j] = 1;
max = 1;
}

for (int i = m - 2; i >= 0; i--)
for (int j = n - 2; j >= 0; j--)
if (matrix[i][j] == '1') {
dp[i][j] = Math.min(dp[i + 1][j + 1],
Math.min(dp[i + 1][j], dp[i][j + 1])) + 1;
max = Math.max(max, dp[i][j]);
}
return max * max;
}
}

• 本文已收录于以下专栏：

## [Leetcode]Maximal Square

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret...

## Leetcode 221. Maximal Square

221. Maximal Square Total Accepted: 29932 Total Submissions: 126159 Difficulty: Medium ...

## Leetcode Maximal Square

Leetcode Maximal Square 相关代码，本算法使用dp算法完成，主要思想为：一列列的计算，计算每个元素右上解的最大square，并记录以其为右下角的square的信息（左边’1’的长...

## leetcode 第221题 Maximal Square

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret...
• djinsis
• 2016年03月17日 21:17
• 103

## 《leetCode》： Maximal Square

《leetCode》： Maximal Square题目 Given a 2D binary matrix filled with 0's and 1's, find the largest s...

## [Leetcode 221, Medium] Maximal square

[Leetcode 221, Medium] Maximal square

## Maximal Square -- leetcode

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret...

## Leetcode: Maximal Square

﻿﻿ Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and...

## leetcode题解分析_221. Maximal Square（图文分析)

【题目】题目链接 Given a 2D binary matrix filled with 0’s and 1’s, find the largest square containing only ...

举报原因： 您举报文章：LeetCode Maximal Square 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)