LeetCode Maximal Square

原创 2015年07月09日 21:33:44

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:

方法一:就一个个来咯,从小到大找,len从小到大,如果有len不存在,就返回最大值。

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;
	}
}


方法二:也可以用DP来解决,dp[i][j]记录的是从i,j这个点开始,能走的最大的正方形的边长。所以如果dp[i][j],dp[i+1][j],dp[i][j+1]和dp[i+1][j+1]都等于l,则dp[i][j]=l+1

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;
	}
}


方法三:倒溯法,从最右下角来,如果dp[i][j] = min{dp[i+1][j], dp[i][j+1], dp[i+1][j+1] } +1

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 - Maximal Square

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

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
  • 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个字)