天天看點

【LeetCode從零單排】No221.Maximal Square題目代碼

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:

Return 4.

解法很巧妙,我也是看了discuss,首先列出最左一列和最上面一列,剩下的解法可以看這個

Basic idea is to iterate over all columns and rows of a matrix (starting with i=j=1). If value in a cell>0 and cells to the north, west, and north-west are >0, pick smallest value of those 3 cells, take it's square root, add 1, and assign square of new value to current cell. For example given matrix

Our answer is the largest value in new matrix: 16

/********************************

* 本文來自部落格  “李博Garvin“

******************************************/