add new solutions
This commit is contained in:
parent
f249852923
commit
f297e11859
10 changed files with 373 additions and 0 deletions
39
solutions/15/q1582/solution.go
Normal file
39
solutions/15/q1582/solution.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
// Package q1582 implements a solution for https://leetcode.com/problems/special-positions-in-a-binary-matrix/
|
||||
package q1582
|
||||
|
||||
func numSpecial(mat [][]int) int {
|
||||
hPos := make([]int, len(mat))
|
||||
vPos := make([]int, len(mat[0]))
|
||||
for i := range vPos {
|
||||
vPos[i] = -1
|
||||
}
|
||||
|
||||
for row := range mat {
|
||||
hPos[row] = -1
|
||||
for col, val := range mat[row] {
|
||||
if val == 1 {
|
||||
if hPos[row] >= 0 {
|
||||
hPos[row] = -2
|
||||
} else if hPos[row] == -1 {
|
||||
hPos[row] = col
|
||||
}
|
||||
|
||||
if vPos[col] >= 0 {
|
||||
vPos[col] = -2
|
||||
} else if vPos[col] == -1 {
|
||||
vPos[col] = row
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pairs := 0
|
||||
for i := range hPos {
|
||||
if hPos[i] >= 0 && vPos[hPos[i]] == i {
|
||||
pairs++
|
||||
}
|
||||
}
|
||||
return pairs
|
||||
}
|
||||
|
||||
var _ = numSpecial
|
||||
Loading…
Add table
Add a link
Reference in a new issue