Leetcode Note: Go - Special Positions in a Binary Matrix
Special Positions in a Binary Matrix - LeetCode
https://leetcode.com/problems/special-positions-in-a-binary-matrix/
- Go 言語で取り組んだメモ
回答
Go one pass 16 ms, 6.1 MB - Special Positions in a Binary Matrix - LeetCode
https://leetcode.com/problems/special-positions-in-a-binary-matrix/solutions/846275/go-one-pass-16-ms-6-1-mb/
func numSpecial(mat [][]int) int {
rows, columns := len(mat),len(mat[0])
specials := 0
for i := 0; i < rows; i++ {
checkCol := -1
for j := 0; j < columns; j++ {
if mat[i][j] == 1 {
if checkCol == -1 {
checkCol = j
} else {
checkCol = -1
break
}
}
}
if checkCol == -1 {
continue
}
special := 1
for k := 0; k < rows; k++ {
if k == i {
continue
}
if mat[k][checkCol] == 1 {
special = 0
mat[0][checkCol] = 1 // speed up future checkings
break
}
}
specials += special
}
return specials
}