[Swift]LeetCode73. 矩阵置零 | Set Matrix Zeroes
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9928348.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.
Example 1:
Input: [ [1,1,1], [1,0,1], [1,1,1] ] Output: [ [1,0,1], [0,0,0], [1,0,1] ]
Example 2:
Input: [ [0,1,2,0], [3,4,5,2], [1,3,1,5] ] Output: [ [0,0,0,0], [0,4,5,0], [0,3,1,0] ]
Follow up:
- A straight forward solution using O(mn) space is probably a bad idea.
- A simple improvement uses O(m + n) space, but still not the best solution.
- Could you devise a constant space solution?
给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0。请使用原地算法。
示例 1:
输入: [ [1,1,1], [1,0,1], [1,1,1] ] 输出: [ [1,0,1], [0,0,0], [1,0,1] ]
示例 2:
输入: [ [0,1,2,0], [3,4,5,2], [1,3,1,5] ] 输出: [ [0,0,0,0], [0,4,5,0], [0,3,1,0] ]
进阶:
- 一个直接的解决方案是使用 O(mn) 的额外空间,但这并不是一个好的解决方案。
- 一个简单的改进方案是使用 O(m + n) 的额外空间,但这仍然不是最好的解决方案。
- 你能想出一个常数空间的解决方案吗?
40ms
1 class Solution { 2 func setZeroes(_ matrix: inout [[Int]]) { 3 var rows = [Int]() 4 var cols = [Int]() 5 6 for i in 0 ..< matrix.count { 7 for j in 0 ..< matrix[i].count { 8 if matrix[i][j] == 0 { 9 rows.append(i) 10 cols.append(j) 11 } 12 } 13 } 14 // Set rows to 0 15 for col in cols { 16 for i in 0 ..< matrix.count { 17 matrix[i][col] = 0 18 } 19 } 20 // Set cols to 0 21 for row in rows { 22 for j in 0 ..< matrix[row].count { 23 matrix[row][j] = 0 24 } 25 } 26 } 27 }
44ms
1 class Solution { 2 func setZeroes(_ matrix: inout [[Int]]) { 3 for i in 0..<matrix.count { 4 for j in 0..<matrix[i].count { 5 if matrix[i][j] == 0 { 6 matrix[i][j] = -9999 7 } 8 } 9 } 10 11 for i in 0..<matrix.count { 12 for j in 0..<matrix[i].count { 13 if matrix[i][j] == -9999 { 14 for c in 0..<matrix[i].count { 15 if (matrix[i][c] != -9999) { 16 matrix[i][c] = 0 17 } 18 } 19 for r in 0..<matrix.count { 20 if (matrix[r][j] != -9999) { 21 matrix[r][j] = 0 22 } 23 } 24 matrix[i][j] = 0 25 } 26 } 27 } 28 } 29 }
44ms
1 class Solution { 2 func setZeroes(_ matrix: inout [[Int]]) { 3 var rows = [Int]() 4 var cols = [Int]() 5 for i in 0..<matrix.count { 6 for j in 0..<matrix[i].count { 7 if matrix[i][j] == 0 { 8 rows.append(i) 9 cols.append(j) 10 } 11 } 12 } 13 for row in rows { 14 matrix[row] = Array(repeating: 0, count: matrix[row].count) 15 } 16 for col in cols { 17 for i in 0..<matrix.count { 18 matrix[i][col] = 0 19 } 20 } 21 } 22 }
48ms
1 class Solution { 2 func setZeroes(_ matrix: inout [[Int]]) { 3 if matrix.count == 0 { 4 return 5 } 6 7 //查找第一行是否有0 8 var row0HasZore = false 9 for value in matrix[0] { 10 if value == 0 { 11 row0HasZore = true 12 break 13 } 14 } 15 16 if matrix.count > 1 { 17 //查找每一行 18 for i in 1..<matrix.count { 19 var rowiHasZore = false 20 //查找该行是否有0,并置第一行该列数为0 21 for j in 0..<matrix[0].count { 22 if matrix[i][j] == 0 { 23 rowiHasZore = true 24 matrix[0][j] = 0 25 } 26 } 27 //如果该行有0,就置该行所有数为0 28 if rowiHasZore { 29 matrix[i].replaceSubrange(0..<matrix[0].count, with: [Int](repeating: 0, count: matrix[0].count)) 30 } 31 } 32 //查找第一行是否有0,有则把整列赋值0 33 for j in 0..<matrix[0].count { 34 if matrix[0][j] == 0 { 35 for i in 0..<matrix.count { 36 matrix[i][j] = 0 37 } 38 } 39 } 40 } 41 //如果第一行有0,则把该行赋值为0 42 if row0HasZore { 43 matrix[0].replaceSubrange(0..<matrix[0].count, with: [Int](repeating: 0, count: matrix[0].count)) 44 } 45 } 46 }
76ms
1 class Solution { 2 func setZeroes(_ matrix: inout [[Int]]) { 3 var rows = [Bool](repeating: false, count: matrix.count) 4 var cols = [Bool](repeating: false, count: matrix[0].count) 5 for (i, row) in matrix.enumerated() { 6 for (j, num) in row.enumerated() { 7 if num == 0 { 8 rows[i] = true 9 cols[j] = true 10 } 11 } 12 } 13 for (i, row) in matrix.enumerated() { 14 for (j, _) in row.enumerated() { 15 if rows[i] || cols[j] { 16 matrix[i][j] = 0 17 } 18 } 19 } 20 } 21 }
160ms
1 class Solution { 2 func setZeroes(_ matrix: inout [[Int]]) { 3 var isCol:Bool = false 4 var R:Int = matrix.count 5 var C:Int = matrix[0].count 6 for i in 0..<R 7 { 8 //因为第一行和第一列的第一个单元是相同的,即矩阵[0][0] 9 //我们可以为第一行/列使用一个附加变量。 10 //对于这个解决方案,我们使用第一列的附加变量。 11 //使用第一行的矩阵[0][0] 12 if matrix[i][0] == 0 {isCol = true} 13 for j in 1..<C 14 { 15 //如果元素为零,则将相应行和列的第一个元素设置为0 16 if matrix[i][j] == 0 17 { 18 matrix[0][j] = 0 19 matrix[i][0] = 0 20 } 21 } 22 } 23 //再次迭代数组并使用第一行和第一列,更新元素 24 for i in 1..<R 25 { 26 for j in 1..<C 27 { 28 if matrix[i][0] == 0 || matrix[0][j] == 0 29 { 30 matrix[i][j] = 0 31 } 32 } 33 } 34 //是否需要将第一行设置为0 35 if matrix[0][0] == 0 36 { 37 for j in 0..<C 38 { 39 matrix[0][j] = 0 40 } 41 } 42 //是否需要将第一列设置为0 43 if isCol 44 { 45 for i in 0..<R 46 { 47 matrix[i][0] = 0 48 } 49 } 50 } 51 }
236ms
1 class Solution { 2 func setZeroes(_ matrix: inout [[Int]]) { 3 4 for i in matrix.indices { 5 for j in matrix[i].indices { 6 //print(i,j, matrix[i][j]) 7 if matrix[i][j] == 0 { 8 helper(&matrix, i, j, 1) 9 helper(&matrix, i, j, 2) 10 helper(&matrix, i, j, 3) 11 helper(&matrix, i, j, 4) 12 } 13 } 14 } 15 16 //print(matrix) 17 for i in matrix.indices { 18 for j in matrix[i].indices { 19 if matrix[i][j] == Int.max { 20 matrix[i][j] = 0 21 } 22 } 23 } 24 } 25 26 func helper(_ matrix: inout[[Int]], _ i: Int, _ j: Int, _ dir: Int) { 27 //print(i, j, dir) 28 var i = i 29 var j = j 30 if dir == 1 { 31 while i >= 0 { 32 if (matrix[i][j] != 0) { 33 matrix[i][j] = Int.max 34 } 35 36 i -= 1 37 } 38 } else if dir == 2 { 39 while i < matrix.count { 40 if (matrix[i][j] != 0) { 41 matrix[i][j] = Int.max 42 } 43 i += 1 44 } 45 } else if dir == 3 { 46 while j >= 0 { 47 if (matrix[i][j] != 0) { 48 matrix[i][j] = Int.max 49 } 50 j -= 1 51 } 52 } else { 53 while j < matrix[i].count { 54 if (matrix[i][j] != 0) { 55 matrix[i][j] = Int.max 56 } 57 j += 1 58 } 59 } 60 } 61 }