1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-10 00:09:06 +01:00
LeetCode/swift/special-positions-in-a-binary-matrix.swift

26 lines
665 B
Swift
Raw Normal View History

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
}
}