龙空技术网

go 实现LeetCode算法题:最大正方形

aurorac 80

前言:

而今看官们对“算法题最大正方形”可能比较讲究,兄弟们都想要知道一些“算法题最大正方形”的相关文章。那么小编也在网上网罗了一些有关“算法题最大正方形””的相关内容,希望我们能喜欢,小伙伴们一起来了解一下吧!

 §§ --- Array/Maximal Square/maximal_square.go §§ 1000+// Package Array Question 225+// Author:  chenyu+// Date : 2019.7.29+// Describle:最大正方形+// 时间复杂度O(mn)+package Array++func maximalSquare(matrix [][]byte) int {+	maxSide := 0+	if len(matrix) == 0 {+		return 0+	}+	rows := len(matrix)+	columns := len(matrix[0])++	for i := 0; i < rows; i++ {+		for j := 0; j < columns; j++ {+			if matrix[i][j] == '1' {+				maxSide = maxInt(maxSide, 1)+				curMaxSide := minInt(rows-i, columns-j)+				for k := 1; k < curMaxSide;k++ {+					flag := true+					if matrix[i+k][j+k] == '0' {+						break+					}+					for m := 0; m < k; m++ {+						if matrix[i+k][j+m] == '0' || matrix[i+m][j+k] == '0' {+							flag = false+							break+						}+					}+					if flag {+						maxSide = maxInt(maxSide, k+1)+					} else {+						break+					}+				}+			}+		}+	}+	maxSquare := maxSide * maxSide+	return maxSquare+}++func maxInt(a,b int) int {+	if a > b {+		return a+	}+	return b+}++func minInt(a,b int) int {+	if a < b {+		return a+	}+	return b+}

标签: #算法题最大正方形