LeetCode/swift/special-positions-in-a-binary-matrix.swift
2023-12-13 18:06:57 +01:00

25 lines
665 B
Swift

class Solution {
func numSpecial(_ mat: [[Int]]) -> Int {
let m = mat.count
let n = mat[0].count
var rows: [Int] = Array(repeating: 0, count: m)
var cols: [Int] = Array(repeating: 0, count: n)
for (y, x) in (0..<m).flatMap { y in (0..<n).map {x in (y, x) } } {
if mat[y][x] == 1 {
rows[y] += 1
cols[x] += 1
}
}
var count = 0
for (y, x) in (0..<m).flatMap { y in (0..<n).map {x in (y, x) } } {
if mat[y][x] == 1 && rows[y] == 1 && cols[x] == 1 {
count += 1
}
}
return count
}
}