Skip to content

Commit 6456d30

Browse files
authored
Merge pull request neetcode-gh#3743 from drxlx/0073-set-matrix-zeroes
Create 0073-set-matrix-zeroes.swift
2 parents dd6ec45 + b00d70a commit 6456d30

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy