-
Notifications
You must be signed in to change notification settings - Fork 21
/
SetMatrixZeroes.swift
49 lines (40 loc) 路 1.12 KB
/
SetMatrixZeroes.swift
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
40
41
42
43
44
45
46
47
48
49
//
// SetMatrixZeroes.swift
// LeetCode
//
// Created by 榛庤档澶儙 on 27/11/2017.
// Copyright 漏 2017 lizhaotailang. All rights reserved.
//
// Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
//
// Accepted. See [SetMatrixZeroes](./LeetCodeTests/SetMatrixZeroesTests.swift) for test cases.
//
import Foundation
class SetMatrixZeroes {
func setZeroes(_ matrix: inout [[Int]]) {
if matrix.isEmpty || matrix[0].isEmpty {
return
}
var row = Set<Int>.init()
var column = Set<Int>.init()
for i in matrix.indices {
for j in matrix[0].indices {
if matrix[i][j] == 0 {
row.insert(i)
column.insert(j)
}
}
}
for i in row {
for k in matrix[0].indices {
matrix[i][k] = 0
}
}
for i in column {
for k in matrix.indices {
matrix[k][i] = 0
}
}
// print(matrix)
}
}