题目

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. Wireshark数据抓包教程之安装Wireshark

    Wireshark数据抓包教程之安装Wireshark 安装Wireshark 通过上一节的学习可以根据自己的操作系统来下载安装Wireshark了.本书中已开发版1.99.7(中文版)为主,下面介绍 ...

  2. 【WIN10】WIN2D——圖像處理

    源碼下載:http://yunpan.cn/c3iNuHFFAcr8h  访问密码 8e48 還是先來看下截圖: 實現了幾個效果:放大.縮小.旋轉.左右翻轉.上下翻轉,亮度變化.灰度圖.對比度.高斯模 ...

  3. IllegalArgumentException: Unmatched braces in the pattern.

    IllegalArgumentException: Unmatched braces in the pattern. 非法参数异常. 不匹配的 吊带 在 样品 中. === 没有管.  项目一直卡在d ...

  4. BZOJ.1076.[SCOI2008]奖励关(概率DP 倒推)

    题目链接 BZOJ 洛谷 真的题意不明啊.. \(Description\) 你有k次选择的机会,每次将从n种物品中随机一件给你,你可以选择选或不选.选择它会获得这种物品的价值:选择一件物品前需要先选 ...

  5. 使用 IntraWeb (12) - 基本控件之 TIWGradButton、TIWImageButton

    TIWGradButton.TIWImageButton 分别是有颜色梯度变化按钮和图像按钮. TIWGradButton 所在单元及继承链: IWCompGradButton.TIWGradButt ...

  6. 玩转ptrace(转)

    下面是转帖的内容,写的很详细.但是不同的linux发行版中头文件的路径和名称并不相同.如在某些发行版中<linux/user.h>就不存在,其中定义的变量出现在<asm/ptrace ...

  7. .Net Discovery 系列之一--string从入门到精通(上)

    string是一种很特殊的数据类型,它既是基元类型又是引用类型,在编译以及运行时,.Net都对它做了一些优化工作,正式这些优化工作有时会迷惑编程人员,使string看起来难以琢磨,这篇文章分上下两章, ...

  8. Java 获取客户端IP

    像移动网关一样,iisforward这个ISAPI过滤器也会对request对象进行再包装,附加一些WLS要用的头信息.这种情况下,直接用request.getRemoteAddr()是无法取到真正的 ...

  9. python测试开发django-37.外键(ForeignKey)查询

    前言 前面在admin后台页面通过设置外键,可以选择下拉框的选项,本篇主要讲解关于外键(ForeignKey)的查询 models设计 在上一篇的基础上新增一个BankName表,Card表通过外键关 ...

  10. poj Kaka&#39;s Matrix Travels

    Kaka's Matrix Travels 题目: 给出一个矩阵.求仅仅能向下或者向右的情况下能得到的最大和.一般的是指遍历一次,而这个是能够反复走K次.每经过一次后就把该点设为0.求最大和. 算法: ...