标签:style class code amp var i++ return matrix solution
Given a 2D binary matrix filled with 0‘s and 1‘s, find the largest square containing only 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
Return 4.
1 public class Solution { 2 public int MaximalSquare(char[,] matrix) { 3 int rows = matrix.GetLength(0), cols = matrix.GetLength(1); 4 5 var dp = new int[rows + 1, cols + 1]; 6 int max = 0; 7 8 for (int i = 0; i < rows; i++) 9 { 10 for (int j = 0; j < cols; j++) 11 { 12 if (matrix[i, j] == ‘0‘) 13 { 14 dp[i + 1, j + 1] = 0; 15 } 16 else 17 { 18 dp[i + 1, j + 1] = Math.Min(Math.Min(dp[i, j + 1], dp[i + 1, j]), dp[i, j]) + 1; 19 max = Math.Max(max, dp[i + 1, j + 1]); 20 } 21 } 22 } 23 24 return max * max; 25 } 26 }
标签:style class code amp var i++ return matrix solution
原文地址:http://www.cnblogs.com/liangmou/p/7953370.html