随笔- 509  文章- 0  评论- 151  阅读- 22万 

Set Matrix Zeroes

2014.2.13 22:15

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

click to show follow up.

Follow up:

Did you use extra space?
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?

Solution1:

  The O(m * n) solution is unacceptable and unnecessary, so we'll write the O(m + n) version first.

  The extra O(n) and O(m) spaces are used to mark if this row or column contains 0.

  Code is relatively simple, please see for yourself.

  Time complexity is O(n * m), space complexity is O(n + m).

Accepted code:

复制代码
 1 // 1AC, O(m + n) solution.
 2 class Solution {
 3 public:
 4     void setZeroes(vector<vector<int> > &matrix) {
 5         int n, m;
 6         
 7         n = matrix.size();
 8         if (n == 0) {
 9             return;
10         }
11         m = matrix[0].size();
12         if (m == 0) {
13             return;
14         }
15 
16         int i, j;
17         vector<int> row_zero, col_zero;
18         
19         for (i = 0; i < n; ++i) {
20             row_zero.push_back(0);
21         }
22         for (j = 0; j < m; ++j) {
23             col_zero.push_back(0);
24         }
25         
26         for (i = 0; i < n; ++i) {
27             for (j = 0; j < m; ++j) {
28                 if (matrix[i][j] == 0) {
29                     row_zero[i] = 1;
30                     col_zero[j] = 1;
31                 }
32             }
33         }
34         
35         for (i = 0; i < n; ++i) {
36             if (row_zero[i]) {
37                 for (j = 0; j < m; ++j) {
38                     matrix[i][j] = 0;
39                 }
40             }
41         }
42         
43         for (j = 0; j < m; ++j) {
44             if (col_zero[j]) {
45                 for (i = 0; i < n; ++i) {
46                     matrix[i][j] = 0;
47                 }
48             }
49         }
50         
51         row_zero.clear();
52         col_zero.clear();
53     }
54 };
复制代码

Solution2:

  Since the problem requires an algorithm using O(1) space, we'll have to improve the previous version.

  The matrix itself takes O(n * m) space, so we might take one row and one column from it as the marker array.

  With one row and column picked out as marker array, we'll need two extra markers for this row and column. Let them be the first row and first column.

  The code will be longer, with the 1st and 2st~nth rows processed separately, likewise for columns.

  Time complexity is O(n * m), space complexity is O(1).

Accepted code:

复制代码
 1 // 1WA, 1AC, use the first row and column as markers.
 2 class Solution {
 3 public:
 4     void setZeroes(vector<vector<int> > &matrix) {
 5         int n, m;
 6         
 7         n = matrix.size();
 8         if (n == 0) {
 9             return;
10         }
11         m = matrix[0].size();
12         if (m == 0) {
13             return;
14         }
15 
16         bool c0_is_zero, r0_is_zero;
17         int i, j;
18         
19         c0_is_zero = false;
20         for (i = 0; i < n; ++i) {
21             if (matrix[i][0] == 0) {
22                 c0_is_zero = true;
23                 break;
24             }
25         }
26         
27         r0_is_zero = false;
28         for (i = 0; i < m; ++i) {
29             if (matrix[0][i] == 0) {
30                 r0_is_zero = true;
31                 break;
32             }
33         }
34         
35         for (i = 1; i < n; ++i) {
36             for (j = 1; j < m; ++j) {
37                 if (matrix[i][j] == 0) {
38                     // use 0th row and column as markers
39                     matrix[i][0] = 0;
40                     matrix[0][j] = 0;
41                 }
42             }
43         }
44         
45         for (i = 1; i < n; ++i) {
46             if (matrix[i][0] == 0) {
47                 for (j = 1; j < m; ++j) {
48                     matrix[i][j] = 0;
49                 }
50             }
51         }
52         
53         for (j = 1; j < m; ++j) {
54             if (matrix[0][j] == 0) {
55                 for (i = 1; i < n; ++i) {
56                     matrix[i][j] = 0;
57                 }
58             }
59         }
60         
61         if (r0_is_zero) {
62             for (j = 0; j < m; ++j) {
63                 matrix[0][j] = 0;
64             }
65         }
66         if (c0_is_zero) {
67             for (i = 0; i < n; ++i) {
68                 matrix[i][0] = 0;
69             }
70         }
71     }
72 };
复制代码

 

 posted on   zhuli19901106  阅读(218)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示