Created
May 9, 2021 07:19
-
-
Save Roshankumar350/18bf70778e4d2218efd242dd326aea97 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
func setZeroes(_ matrix: inout [[Int]]) { | |
if matrix.isEmpty { | |
return | |
} | |
struct Coordinate: Equatable, Hashable { | |
var row: Int | |
var column: Int | |
} | |
var zerosIndex = Set<Coordinate>() | |
for (eachRowIndex, eachRow) in matrix.enumerated() { | |
for (eachColumnIndex, eachColumnValue) in eachRow.enumerated() { | |
if eachColumnValue == 0 { | |
let coordinate = Coordinate(row: eachRowIndex, column: eachColumnIndex) | |
zerosIndex.insert(coordinate) | |
} | |
} | |
} | |
for eachCoordinates in zerosIndex { | |
for (eachRowIndex, eachRow) in matrix.enumerated() { | |
for (eachColumn, eachColumnValue) in eachRow.enumerated() { | |
if eachCoordinates.column == eachColumn { | |
matrix[eachRowIndex][eachCoordinates.column] = 0 | |
} | |
if eachCoordinates.row == eachRowIndex { | |
matrix[eachCoordinates.row][eachColumn] = 0 | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment