Skip to content

Commit 474f3d5

Browse files
committed
Solution Set matrix zeroes
1 parent 32faa56 commit 474f3d5

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

set-matrix-zeroes/doitduri.swift

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class Solution {
2+
func setZeroes(_ matrix: inout [[Int]]) {
3+
let m = matrix.count
4+
let n = matrix[0].count
5+
6+
var firstRowHasZero = false
7+
var firstColHasZero = false
8+
9+
for i in 0..<m {
10+
if matrix[i][0] == 0 {
11+
firstColHasZero = true
12+
break
13+
}
14+
}
15+
16+
for j in 0..<n {
17+
if matrix[0][j] == 0 {
18+
firstRowHasZero = true
19+
break
20+
}
21+
}
22+
23+
for i in 1..<m {
24+
for j in 1..<n {
25+
if matrix[i][j] == 0 {
26+
matrix[i][0] = 0
27+
matrix[0][j] = 0
28+
}
29+
}
30+
}
31+
32+
for i in 1..<m {
33+
if matrix[i][0] == 0 {
34+
for j in 1..<n {
35+
matrix[i][j] = 0
36+
}
37+
}
38+
}
39+
40+
for j in 1..<n {
41+
if matrix[0][j] == 0 {
42+
for i in 1..<m {
43+
matrix[i][j] = 0
44+
}
45+
}
46+
}
47+
48+
if firstRowHasZero {
49+
for j in 0..<n {
50+
matrix[0][j] = 0
51+
}
52+
}
53+
54+
if firstColHasZero {
55+
for i in 0..<m {
56+
matrix[i][0] = 0
57+
}
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)