Set Matrix Zeroes
来源:互联网 发布:淘宝旧版本4.0.0下载 编辑:程序博客网 时间:2024/05/01 12:39
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
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?
先遍历一遍矩阵,如果某个元素为0,则设置其所在的行和列的标记数组为true。(row[i] =true; column[j]= true)
然后第二遍遍历矩阵,如果row[i] 或者column[j]为true, 则设置matrix[i][j]=0;
void setZeroes(vector<vector<int> > &matrix){ int m=matrix.size(); int n=matrix[0].size(); bool *row = new bool[m]; memset(row,false,m); bool *column = new bool[n]; memset(column,false,n); for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { if(matrix[i][j]==0) { row[i]=true; column[j]=true; } } } for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { if(row[i] || column[j]) matrix[i][j]=0; } }}
0 0
- LeetCode: Set Matrix Zeroes
- Set Matrix Zeroes
- Set Matrix Zeroes
- LeetCode Set Matrix Zeroes
- [Leetcode] Set Matrix Zeroes
- LeetCode: Set Matrix Zeroes
- Set Matrix Zeroes
- Leetcode: Set Matrix Zeroes
- [LeetCode] Set Matrix Zeroes
- Set Matrix Zeroes
- Set Matrix Zeroes
- [LeetCode]Set Matrix Zeroes
- Set Matrix Zeroes
- Set Matrix Zeroes
- Set Matrix Zeroes
- [leetcode]Set Matrix Zeroes
- Leetcode:Set Matrix Zeroes
- Set Matrix Zeroes
- 旋转卡壳法求凸包的直径
- 第三方
- 实现Cocos2d-x追踪子弹
- jsp页面倒计时、javascript的MD5加密
- android学习记录-activity跳转
- Set Matrix Zeroes
- div+ajax代替iframe
- Java中this的使用方法
- Android 设置Activity 全屏的方法
- Linux学习:第五章-Linux用户和用户组管理
- Linux 多线程应用中如何编写安全的信号处理函数
- redis 故障时排查命令
- linux socket select recv ready
- nyoj-Color the necklace(Ploya定理 + 欧拉函数 + 扩展欧几里得(求逆元))