[Swift]LeetCode73. 矩陣置零 | Set Matrix Zeroes

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-supxhyno-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.git

Example 1:github

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:app

輸入: 
[
  [1,1,1],
  [1,0,1],
  [1,1,1]
]
輸出: 
[
  [1,0,1],
  [0,0,0],
  [1,0,1]
]

示例 2:ide

輸入: 
[
  [0,1,2,0],
  [3,4,5,2],
  [1,3,1,5]
]
輸出: 
[
  [0,0,0,0],
  [0,4,5,0],
  [0,3,1,0]
]

進階:idea

  • 一個直接的解決方案是使用  O(mn) 的額外空間,但這並非一個好的解決方案。
  • 一個簡單的改進方案是使用 O(m + n) 的額外空間,但這仍然不是最好的解決方案。
  • 你能想出一個常數空間的解決方案嗎?

40msspa

 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 }
相關文章
相關標籤/搜索