Leetcode 073 Set Matrix Zeroes
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?
1.O(m+n)space,两个数组m和n分别存储第i行和第j列有没有零,比较慢
2.constant space ,利用矩阵的第零行和第零列分别存储第i行和第j列有没有零,需要注意的是再用两个变量row和column来表示第零行和第零列是否有零存在,因为这是边界情况
public class S073 {
public void setZeroes(int[][] matrix) {
//O(m+n) space slow
/* int [] m = new int[matrix.length];
int [] n = new int[matrix[0].length];
for (int i = 0;i<matrix.length;i++) {
for (int j = 0;j<matrix[0].length;j++) {
if (matrix[i][j] == 0) {
m[i] = -1;
n[j] = -1;
}
}
}
for (int i = 0;i<matrix.length;i++) {
if (m[i] == -1) {
for (int j = 0;j<matrix[0].length;j++) {
matrix[i][j] = 0;
if (n[j] == -1) {
for (int k = 0;k<matrix.length;k++) {
matrix[k][j] = 0;
}
}
}
}
} */
//利用矩阵的第零行和第零列分别来存每一列和每一行是否有零存在
int row = -1;
int column = -1;
for (int i = 0;i<matrix.length;i++) {
for (int j = 0;j<matrix[0].length;j++) {
if (matrix[i][j] == 0) {
if (i == 0 ) {
row = 0;
}
if (j == 0) {
column = 0;
}
matrix[0][j] = 0; //第零行
matrix[i][0] = 0; //第零列
}
}
}
for (int i = 1;i<matrix.length;i++) {
if (matrix[i][0] == 0) {
for (int j = 1;j<matrix[0].length;j++) {
matrix[i][j] = 0;
}
}
}
for (int j = 1;j<matrix[0].length;j++) {
if (matrix[0][j] == 0) {
for (int i = 1;i<matrix.length;i++) {
matrix[i][j] = 0;
}
}
}
if (row == 0) {
for (int i = 0;i<matrix[0].length;i++) {
matrix[0][i] = 0;
}
}
if (column == 0) {
for (int i = 0;i<matrix.length;i++) {
matrix[i][0] = 0;
}
}
}
}
Leetcode 073 Set Matrix Zeroes的更多相关文章
- Java for LeetCode 073 Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 解题思路: ...
- 【leetcode】Set Matrix Zeroes(middle)
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用 ...
- 073 Set Matrix Zeroes 矩阵置零
给定一个 m x n 的矩阵,如果一个元素为 0 ,则将这个元素所在的行和列都置零.你有没有使用额外的空间?使用 O(mn) 的空间不是一个好的解决方案.使用 O(m + n) 的空间有所改善,但仍不 ...
- [LeetCode] 73. Set Matrix Zeroes 矩阵赋零
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. Exampl ...
- Leetcode#73 Set Matrix Zeroes
原题地址 用矩形的第一行和第一列充当mask 代码: void setZeroes(vector<vector<int> > &matrix) { ].empty()) ...
- [LeetCode] 73. Set Matrix Zeroes 解题思路
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow ...
- 【Leetcode】Set Matrix Zeroes
给定一个m x n的矩阵,如果某个元素为0,则把该元素所在行和列全部置0. Given a m x n matrix, if an element is 0, set its entire row a ...
- 【LeetCode】Set Matrix Zeroes 解题报告
今天看到CSDN博客的勋章换了图表,同一时候也添加显示了博客等级,看起来都听清新的,感觉不错! [题目] Given a m x n matrix, if an element is 0, set i ...
- leetcode[73] Set Matrix Zeroes 将矩阵置零
给定一个矩阵,把零值所在的行和列都置为零.例如: 1 2 3 1 3 1 1 1 操作之后变为 1 3 0 0 0 1 1 方法1: 赋值另存一个m*n的矩阵,在原矩阵为零的值相应置新的矩阵行和列为零 ...
随机推荐
- SUSElinux的pam模块中$ISA变量的作用
目的:限制非wheel用户切换至root 方法:vi /etc/pam.d/su文件,增加如下两行 auth sufficient /lib/security/pam_rootok.soauth re ...
- Linux连接xshell找不到IP信息
虚拟机环境下的Linux连接xshell的网络连接找不到eth0(IP)信息的解决方法 1 输入ifconfig,如果有eth0信息,直接填写eth0上面的IP信息 2 输入ifconfig ...
- 关于WIN7 家庭版 iis 部署问题
预装Win7家庭普通版系统的iis部署 必先升级为win7 预计10分钟因个人电脑而异 Win7家庭普通版系统的机器可免费升级为旗舰版.(WIN7任何低版本的系统 都可以升级到旗舰版) 开始的步骤: ...
- 【优先队列】-HDU4546比赛难度
比赛难度 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submis ...
- js中的错误检测
<!DOCTYPE html> <html> <body> <script> function myFunction() { try { var x=d ...
- 绿色版的mysql安装配置方式
解压下载好的压缩包 copy 一份my-default.ini改名字为my.ini为mysql的配置文件 打开my.ini 修改配置文件 默认的原版文件为 # For advice on how to ...
- 每天学习一点点...css...
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- PHP22期基础班技术总结
- 关于去除input type='file'改变组件的默认样式换成自己需要的样式的解决方案
在工作中时常会遇到如需要上传功能的按钮,而不像需要系统默认的样式时候,可以采取以下的解决方案: <img onclick="getElementById('file').click() ...
- FORM提交请求后自动打开输出EDITOR_PKG.REPORT
DECLARE p_mode_request_id number := 1; p_ERRBUF VARCHAR2(250); p_RETCODE NUMBER; lv_msg varchar2(50) ...