problems(swift): add “1582. Special Positions in a Binary Matrix”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
a48dd4765e
commit
eddb4c1a55
1 changed files with 25 additions and 0 deletions
25
swift/special-positions-in-a-binary-matrix.swift
Normal file
25
swift/special-positions-in-a-binary-matrix.swift
Normal file
|
@ -0,0 +1,25 @@
|
|||
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
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue