<LeetCode OJ> 73. Set Matrix Zeroes
2016-03-07 20:14
344 查看
Total Accepted: 60832 Total
Submissions: 184016 Difficulty: Medium
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?
Subscribe to see which companies asked this question
Hide Tags
Array
Hide Similar Problems
(M) Game of Life
分析:
题目给定一个m∗n矩阵,要求将矩阵中值为0的元素所在的整行、整列元素均赋值为0;
重点是要求算法是原地处理,并且算法最好是空间复杂度为O(1)
本题还是比较善良的,比较简单,代码注释一目了然。
注意以下代码为简单起见:使用了空间为:O(m+n)
学习别人的代码:
原文地址:http://blog.csdn.net/fly_yr/article/details/48499177
他的具体思路如下:
利用矩阵的第一行和第一列来作为辅助空间使用,不用开辟新的存储空间。
方法就是:
1.先确定第一行和第一列是否需要清零
即,看看第一行中是否有0,记下来;也同时记下来第一列中有没有0。
2.扫描剩下的矩阵元素,如果遇到了0,就将对应的第一行和第一列上的元素赋值为0
这里不用担心会将本来第一行或第一列的1改成了0,因为这些值最后注定要成为0的,比如matrix[i][j]==0,那么matrix[i][0]处在第i行,matrix[0][j]处于第j列,最后都要设置为0的。
3.根据第一行和第一列的信息,已经可以将剩下的矩阵元素赋值为结果所需的值了即,拿第一行为例,如果扫描到一个0,就将这一列都清0.
4.根据1中确定的状态,处理第一行和第一列。
如果最开始得到的第一行中有0的话,就整行清零。同理对列进行处理。
注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50821973
原作者博客:http://blog.csdn.net/ebowtang
本博客LeetCode题解索引:/article/3664871.html
Submissions: 184016 Difficulty: Medium
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?
Subscribe to see which companies asked this question
Hide Tags
Array
Hide Similar Problems
(M) Game of Life
分析:
题目给定一个m∗n矩阵,要求将矩阵中值为0的元素所在的整行、整列元素均赋值为0;
重点是要求算法是原地处理,并且算法最好是空间复杂度为O(1)
本题还是比较善良的,比较简单,代码注释一目了然。
注意以下代码为简单起见:使用了空间为:O(m+n)
class Solution { public: void setZeroes(vector<vector<int>>& matrix) { int row=matrix.size(); int col=matrix[0].size(); vector<int> recordrow(row,0); vector<int> recordcol(col,0); //一,记录到底哪一行,哪一列应该被置零 for(int i=0;i<row;i++) { for(int j=0;j<col;j++){ if(matrix[i][j]==0){ recordrow[i]=1; recordcol[j]=1; } } } //二,根据对相应recordrow置零该行 for(int i=0;i<row;i++) { if(recordrow[i]==1) { for(int j=0;j<col;j++) matrix[i][j]=0; } } //三,根据对相应recordcol置零该列 for(int i=0;i<col;i++) { if(recordcol[i]==1) { for(int j=0;j<row;j++) matrix[j][i]=0; } } } };
学习别人的代码:
原文地址:http://blog.csdn.net/fly_yr/article/details/48499177
他的具体思路如下:
利用矩阵的第一行和第一列来作为辅助空间使用,不用开辟新的存储空间。
方法就是:
1.先确定第一行和第一列是否需要清零
即,看看第一行中是否有0,记下来;也同时记下来第一列中有没有0。
2.扫描剩下的矩阵元素,如果遇到了0,就将对应的第一行和第一列上的元素赋值为0
这里不用担心会将本来第一行或第一列的1改成了0,因为这些值最后注定要成为0的,比如matrix[i][j]==0,那么matrix[i][0]处在第i行,matrix[0][j]处于第j列,最后都要设置为0的。
3.根据第一行和第一列的信息,已经可以将剩下的矩阵元素赋值为结果所需的值了即,拿第一行为例,如果扫描到一个0,就将这一列都清0.
4.根据1中确定的状态,处理第一行和第一列。
如果最开始得到的第一行中有0的话,就整行清零。同理对列进行处理。
class Solution { public: void setZeroes(vector<vector<int>>& matrix) { if (matrix.empty()) return; //求得所给矩阵的行数、列数 int m = matrix.size(); int n = matrix.front().size(); //初始化首行,首列标志位false,代表元素不为0 bool f_row = false, f_col = false; for (int j = 0; j < n; j++) { if (matrix[0][j] == 0) { f_row = true; break; }//if }//for //记录首行、首列的状态 for (int i = 0; i < m; i++) { if (matrix[i][0] == 0) { f_col = true; break; }//if }//for //下面用原矩阵的首行和首列作为本地存储空间,因为首行首列单独处理,所以下标均从1开始 for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { //如果元素(i,j)为0,则将该元素对应的首行位置(0,j)以及首列位置(i,0)值赋为0 if (matrix[i][j] == 0) { matrix[0][j] = 0; matrix[i][0] = 0; }//if }//for }//for //下面根据首行,首列元素值,更新矩阵中的0 for (int j = 1; j < n; j++) { //找到元素为0的坐标,将矩阵中该列元素全部更改为0 if (matrix[0][j] == 0) { for (int i = 0; i < m; i++) matrix[i][j] = 0; }//if } for (int i = 1; i < m; i++) { if (matrix[i][0] == 0) { for (int j = 0; j < n; j++) matrix[i][j] = 0; }//if }//for //最后处理首行和首列 if (f_row) { for (int j = 0; j < n; j++) matrix[0][j] = 0; } if (f_col) { for (int i = 0; i < m; i++) matrix[i][0] = 0; } } };
注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50821973
原作者博客:http://blog.csdn.net/ebowtang
本博客LeetCode题解索引:/article/3664871.html
相关文章推荐
- 同步、异步、阻塞、非阻塞
- C#迭代重载等
- web安全:click jacking
- 辞职申请
- 【HDU-1058】Humble Numbers 丑数
- LeetCode 54 - Spiral Matrix
- Adjacent Node Sum (图的邻接表表示 C语言)
- Edittext ---Android
- 随机森林(Random Foreast)
- web安全:xss && csrf
- Leetcode 119 Pascal's Triangle II 数论递推
- 分布式 session
- 51nod p1201 整数划分
- 黏性tableView 下拉变长 毛玻璃效果渐变
- 51nod p1201 整数划分
- 免费试用阿里云空间和邮箱服务
- LeetCode : Merge Two Sorted Lists [java]
- 331. Verify Preorder Serialization of a Binary Tree
- C#面向对象
- [GDKOI2016]项链