-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy path0073-set-matrix-zeroes.swift
More file actions
39 lines (35 loc) · 927 Bytes
/
0073-set-matrix-zeroes.swift
File metadata and controls
39 lines (35 loc) · 927 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution {
func setZeroes(_ matrix: inout [[Int]]) {
let rows = matrix.count, cols = matrix[0].count
var rowZero = false
for r in 0..<rows {
for c in 0..<cols {
if matrix[r][c] == 0 {
matrix[0][c] = 0
if r > 0 {
matrix[r][0] = 0
} else {
rowZero = true
}
}
}
}
for r in 1..<rows {
for c in 1..<cols {
if matrix[0][c] == 0 || matrix[r][0] == 0 {
matrix[r][c] = 0
}
}
}
if matrix[0][0] == 0 {
for r in 0..<rows {
matrix[r][0] = 0
}
}
if rowZero {
for c in 0..<cols {
matrix[0][c] = 0
}
}
}
}