题目:

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

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的元素,就把该元素对应的行和列上面的元素全都改成0。

思考了半天,还是遍历了两遍,复杂度至少在O(mn):

public void setZeroes(int[][] matrix) {
         int i,j;
         ArrayList<Integer> row = new ArrayList<Integer>();
         ArrayList<Integer> col = new ArrayList<Integer>();        
         //第一个循环,遍历一遍,记录哪些行和哪些列需要改成0
         for (i=0;i < matrix.length;i++)
         { 
          for (j=0;j<matrix[i].length;j++)
          {
           if(matrix[i][j] == 0)
           {
            row.add(i);
            col.add(j);
           }
          }       
         }
       //第二个循环,又遍历一遍,根据需要变为0的行和列修改对应元素
         for (i=0;i < matrix.length;i++)
         { 
          for (j=0;j<matrix[i].length;j++)
          {
           if(row.contains(i)||col.contains(j))
           {
            matrix[i][j] =0;
//            System.out.print("修改第"+i+"行,第"+j+"列");
           } 
          }       
         }
 
     }

题目中提到算法复杂度可以小于 O(m + n),还在研究中...

73. Set Matrix Zeroes的更多相关文章

  1. 【LeetCode】73. Set Matrix Zeroes (2 solutions)

    Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do i ...

  2. Leetcode#73 Set Matrix Zeroes

    原题地址 用矩形的第一行和第一列充当mask 代码: void setZeroes(vector<vector<int> > &matrix) { ].empty()) ...

  3. [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 ...

  4. leetcode[73] Set Matrix Zeroes 将矩阵置零

    给定一个矩阵,把零值所在的行和列都置为零.例如: 1 2 3 1 3 1 1 1 操作之后变为 1 3 0 0 0 1 1 方法1: 赋值另存一个m*n的矩阵,在原矩阵为零的值相应置新的矩阵行和列为零 ...

  5. LeetCode OJ 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. click ...

  6. 【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. Fo ...

  7. 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. 重点是空间复 ...

  8. 【一天一道LeetCode】#73. Set Matrix Zeroes

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  9. 73. Set Matrix Zeroes 把矩阵同一行列的元素都改成0

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

随机推荐

  1. JQuery仿淘宝商家后台管理 之 管理添加分类

    先看一下效果图: 实现功能: 1.打开时加载分类信息,折叠所有分类 2.动态添加子类和父类 3.顺序的调整 4.无刷新删除,添加 5.保存到数据库 下面是HTML代码 : <title>分 ...

  2. 用数组求Fibonacci数列

    #include<stdio.h>int main(){      int a[20]={1,1};      int n=2,i;      for(n=2;n<20;n++)  ...

  3. Jquery中的事件和动画

    在学习Jquery中的过程中我们绝大部分都用到了事件的操作,也可以说事件是Jquery中必不可少的一部分,我们常见的一些事件有单击事件,鼠标事件,键盘事件等等.在Jquery中的学习中为了能使让页面以 ...

  4. 第2月第1天 命令(Command)模式

    http://www.tracefact.net/Design-Pattern/Command.aspx 命令模式把一个请求或者操作封装到一个对象中.命令模式允许系统使用不同的请求把客户端参数化,对请 ...

  5. hash-5.ConcurrentHashMap

    http://www.cnblogs.com/dolphin0520/p/3932905.html有时间细看

  6. 中国天气预报数据API收集

      {"weatherinfo":{"city":"北京","cityid":"101010100" ...

  7. java使用split分隔,需要注意的点

    String severName = "10.6.62.244"; System.out.println(severName.split(".").length ...

  8. 从Objective-C转战C++ Android平台开发实践(C++/Java)

    是否使用虚拟方法 最好在不用“virtual”关键字的情况下声明所有cpp成员方法 但是在写CPP头文件时,请检查有没有父类的方法被当前的工作覆盖.如果有,请确保将这些方法改为虚拟方法. 如果从父类继 ...

  9. Android界面刷新之invalidate与postInvalidate的区别

    Android的invalidate与postInvalidate都是用来刷新界面的. 在UI主线程中,用invalidate():本质是调用View的onDraw()绘制. 主线程之外,用postI ...

  10. git 教程(5)--工作区和暂存区

    Git和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念. 工作区(working directory) 就是你在电脑里能看到的目录,比如我的learngit文件夹就是一个工作区: 版本库 ( ...