[Swift]LeetCode73. 矩陣置零 | Set Matrix Zeroes
阿新 • • 發佈:2018-11-08
constant 矩陣 簡單的 相同 max nbsp span print app
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]]) { 3var 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 }
[Swift]LeetCode73. 矩陣置零 | Set Matrix Zeroes