[leetcode] Set Matrix Zeroes
来源:互联网 发布:淘宝达人如何吸粉 编辑:程序博客网 时间:2024/04/29 22:22
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?
思路:用两个数组来表示行和列是否要置为0
代码:
class Solution {public: void setZeroes(vector<vector<int> > &matrix) { int m=matrix.size(),n=matrix[0].size(); int row[m],col[n]; memset(row,1,sizeof(row)); memset(col,1,sizeof(col)); for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ if(matrix[i][j]==0){ row[i]=0; col[j]=0; } } } for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ if(row[i]==0 || col[j]==0){ matrix[i][j]=0; } } } return; }};
0 0
- LeetCode: Set Matrix Zeroes
- LeetCode Set Matrix Zeroes
- [Leetcode] Set Matrix Zeroes
- LeetCode: Set Matrix Zeroes
- Leetcode: Set Matrix Zeroes
- [LeetCode] Set Matrix Zeroes
- [LeetCode]Set Matrix Zeroes
- [leetcode]Set Matrix Zeroes
- Leetcode:Set Matrix Zeroes
- LeetCode-Set Matrix Zeroes
- [leetcode] Set Matrix Zeroes
- LeetCode - Set Matrix Zeroes
- Set Matrix Zeroes leetcode
- 【Leetcode】Set Matrix Zeroes
- 【leetcode】Set Matrix Zeroes
- Leetcode: Set Matrix Zeroes
- Leetcode Set Matrix Zeroes
- LeetCode | Set Matrix Zeroes
- Linux内核printk日志输出控制
- CentOS vncserver配置
- day123(07.14)
- Json转换利器Gson之实例一-简单对象转化和带泛型的List转化
- CentOS 6.3 SSH很慢的解决方法
- [leetcode] Set Matrix Zeroes
- 黑马学习日记(二)IO流相关重点及使用示例
- Docker:分布式系统的软件工程革命(上)
- Centos 安全配置之 修改sshd配置文件
- OTT企业为何不应向运营商缴费?
- MySQL修改默认存储引擎
- C语言之const关键字修饰
- 黑马程序员——Java基础_数组
- PHP小记:smarty自定义函数