题目

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?

题解

这道题是CC150 1.7上面的原题。可以看上面详尽的解释,我就不写了。

代码如下:

 1     public void setZeroes(int[][] matrix) {

 2         int m = matrix.length;

 3         int n = matrix[0].length;

 4         

 5         if(m==0||n==0)

 6             return;

 7         int[] flagr = new int[m];

 8         int[] flagc = new int[n];

 9         

         for(int i=0;i<m;i++){

             for(int j=0;j<n;j++){

                 if(matrix[i][j]==0){

                     flagr[i]= 1; 

                     flagc[j]= 1;

                 }

             }

         }

         

         for(int i=0;i<m;i++){

             for(int j=0;j<n;j++){

                 if(flagr[i]==1||flagc[j]==1){

                     matrix[i][j]=0;

                 }

             }

         }

     }

另一种方法是不需要额外空间的,代码来自discuss:

 1 public void setZeroes(int[][] matrix) {
 2     int rownum = matrix.length;
 3     if (rownum == 0)  return;
 4     int colnum = matrix[0].length;
 5     if (colnum == 0)  return;
 6 
 7     boolean hasZeroFirstRow = false, hasZeroFirstColumn = false;
 8 
 9     // Does first row have zero?
     for (int j = 0; j < colnum; ++j) {
         if (matrix[0][j] == 0) {
             hasZeroFirstRow = true;
             break;
         }
     }
 
     // Does first column have zero?
     for (int i = 0; i < rownum; ++i) {
         if (matrix[i][0] == 0) {
             hasZeroFirstColumn = true;
             break;
         }
     }
 
     // find zeroes and store the info in first row and column
     for (int i = 1; i < matrix.length; ++i) {
         for (int j = 1; j < matrix[0].length; ++j) {
             if (matrix[i][j] == 0) {
                 matrix[i][0] = 0;
                 matrix[0][j] = 0;
             }
         }
     }
 
     // set zeroes except the first row and column
     for (int i = 1; i < matrix.length; ++i) {
         for (int j = 1; j < matrix[0].length; ++j) {
             if (matrix[i][0] == 0 || matrix[0][j] == 0)  matrix[i][j] = 0;
         }
     }
 
     // set zeroes for first row and column if needed
     if (hasZeroFirstRow) {
         for (int j = 0; j < colnum; ++j) {
             matrix[0][j] = 0;
         }
     }
     if (hasZeroFirstColumn) {
         for (int i = 0; i < rownum; ++i) {
             matrix[i][0] = 0;
         }
     }
 }

Set Matrix Zeroes leetcode java的更多相关文章

  1. Set Matrix Zeroes -- LeetCode

    原题链接: http://oj.leetcode.com/problems/set-matrix-zeroes/ 这是一个矩阵操作的题目,目标非常明白,就是假设矩阵假设有元素为0,就把相应的行和列上面 ...

  2. Spiral Matrix II leetcode java

    题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...

  3. LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors

    1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...

  4. LeetCode: Set Matrix Zeroes 解题报告

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

  5. [LeetCode] 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 细节实现 Set Matrix Zeroes

    Set Matrix Zeroes Total Accepted: 18139 Total Submissions: 58671My Submissions Given a m x n matrix, ...

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

  8. 55. Set Matrix Zeroes

    Set Matrix Zeroes (Link: https://oj.leetcode.com/problems/set-matrix-zeroes/) Given a m x n matrix, ...

  9. [CareerCup] 1.7 Set Matrix Zeroes 矩阵赋零

    1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are ...

随机推荐

  1. 添加第一个控制器(Controllers)

    在MVC体系架构中,输入请求是由控制器Controller来处理的(负责处理浏览器请求,并作出响应).在ASP.NET MVC中Controller本身是一个类(Class)通常继承于System.W ...

  2. 【坐标离散化】AOJ0531- Paint Color

    日文题……一开始被题目骗了以为真的要写文件? 题目大意&&解答戳:❀ #include<iostream> #include<cstdio> #include& ...

  3. MultiByteToWideChar和WideCharToMultiByte

    CString UTF8ToGB2312(CString str) { int len; // UTF8转换成Unicode len = MultiByteToWideChar(CP_UTF8, 0, ...

  4. Android Native层异步消息处理框架

     *本文系作者工作学习总结,尚有不完善及理解不恰当之处,欢迎批评指正* 一.前言 在NuPlayer中,可以发现许多类似于下面的代码: //============================== ...

  5. HDU 5738 Eureka 统计共线的子集个数

    Eureka 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5738 Description Professor Zhang draws n poin ...

  6. 阻止新的csproj工程的dll引用继承

    VisualStudio传统的csproj工程中,引用是没有继承功能的.例如,对于如下一个引用关系 App引用Assembly 1 Assembly 1引用Assembly 2 程序App在没有添加A ...

  7. WinForm通用自动更新器AutoUpdater项目实战

    一.项目背景介绍 最近单位开发一个项目,其中需要用到自动升级功能.因为自动升级是一个比较常用的功能,可能会在很多程序中用到,于是,我就想写一个自动升级的组件,在应用程序中,只需要引用这个自动升级组件, ...

  8. VC2010中"Include Directories" 和 "Additional Include Directories"的区别

    右键一个Project,可以发现有两个地方设置Include的相关目录: 1. VC++ Directories -> Include Directories2. C/C++ -> Gen ...

  9. 基于TQ2440和Qemu的GDB+串口调试(1)

    作者 彭东林 pengdonglin137@163.com   平台 TQ2440 + Linux-4.10.17 Qemu(vexpress-ca9) + Linux-4.10.17   概述 下面 ...

  10. Unity3D实践系列04, 脚本的生命周期

    Unity3D脚本生命周期是指从脚本的最初唤醒到脚本最终销毁的整个过程.生命周期的各个方法被封装到了MonoBehaviour类中.具体来说如下: 1.In Editor Mode 编辑模式 当在编辑 ...