Skip to content

Commit b00d70a

Browse files
committed
Create 0073-set-matrix-zeroes.swift
1 parent 864a32a commit b00d70a

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

swift/0073-set-matrix-zeroes.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
func setZeroes(_ matrix: inout [[Int]]) {
3+
let rows = matrix.count, cols = matrix[0].count
4+
var rowZero = false
5+
6+
for r in 0..<rows {
7+
for c in 0..<cols {
8+
if matrix[r][c] == 0 {
9+
matrix[0][c] = 0
10+
if r > 0 {
11+
matrix[r][0] = 0
12+
} else {
13+
rowZero = true
14+
}
15+
}
16+
}
17+
}
18+
19+
for r in 1..<rows {
20+
for c in 1..<cols {
21+
if matrix[0][c] == 0 || matrix[r][0] == 0 {
22+
matrix[r][c] = 0
23+
}
24+
}
25+
}
26+
27+
if matrix[0][0] == 0 {
28+
for r in 0..<rows {
29+
matrix[r][0] = 0
30+
}
31+
}
32+
33+
if rowZero {
34+
for c in 0..<cols {
35+
matrix[0][c] = 0
36+
}
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)