LeetCode题解-1072
题目:
Flip Columns For Maximum Number of Equal Rows
解答:
func maxEqualRowsAfterFlips(matrix [][]int) int {
// 基础值为1,因为经过一些翻转,至少能保证一行值相同
max := 1
//每一行转变成string类型之后,每个string出现的次数
stringOccurrenceMap := make(map[string]int)
rowNum := len(matrix)
columnNum := len(matrix[0])
var s, ps string
for i := 0; i < rowNum; i ++ {
//将每一行的0,1序列转变成byte数组
var byteArr, polarByteArr []byte
for j := 0; j < columnNum; j ++ {
if matrix[i][j] == 0 {
byteArr = append(byteArr, 0)
polarByteArr = append(polarByteArr, 1)
} else {
byteArr = append(byteArr, 1)
polarByteArr = append(polarByteArr, 0)
}
}
s = string(byteArr)
ps = string(polarByteArr)
stringOccurrenceMap[s]++
stringOccurrenceMap[ps]++
if stringOccurrenceMap[s] > max {
max = stringOccurrenceMap[s]
}
if stringOccurrenceMap[ps] > max {
max = stringOccurrenceMap[ps]
}
}
return max
}
结果:
性能还不错,占用内存击败了100%!