题目

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. Xamarin iOS教程之页面控件

    Xamarin iOS教程之页面控件 Xamarin iOS 页面控件 在iPhone手机的主界面中,经常会看到一排小白点,那就是页面控件,如图2.44所示.它是由小白点和滚动视图组成,可以用来控制翻 ...

  2. DPDK+OpenvSwitch-centos7.4安装

    系统版本 [root@controller ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) DPDK版本: dpdk- ...

  3. BZOJ2655 calc

    拉格朗日插值+dp 直接dp是n立方的,我们考虑优化. dp式子为f[i][j]=f[i-1][j-1]*j*i+f[i-1][j]表示i个元素选j个的答案 然后发现最高次就是2j次,所以我们预处理出 ...

  4. Codeforces Round #372 (Div. 1) A. Plus and Square Root 数学题

    A. Plus and Square Root 题目连接: http://codeforces.com/contest/715/problem/A Description ZS the Coder i ...

  5. Java使用独立数据库连接池(DBCP为例)

    目前,绝大多数的软件系统都会使用数据库,而在软件构建起来之后,访问数据库又成为软件系统性能的短板(I/O操作).一般来说一次访问数据库就需要一个数据库连接.而每次创建数据库连接都需要访问,分配空闲资源 ...

  6. Unity3D对安卓盒子的支持

    一般的安卓盒子主要按键包含 1.方向键:上下左右 2.确认 3.返回 4.音量(Unity无法获取),须要在安卓层将事件发上来,KeyCode = 24,25 基本的函数是 if (Input.Get ...

  7. setInterval设置停止和循环

    原文链接:http://caibaojian.com/setinterval-times.html 需要知道已经经过了多少次或者说过多久就会停止 var timesRun = 0; var inter ...

  8. gdb调试报错:Missing separate debuginfos, use: debuginfo-install glibc-XXX

    解决方案: 1.先修改“/etc/yum.repos.d/CentOS-Debuginfo.repo”文件的 enable=1: 2.使用 sudo yum install glibc 安装: 3.使 ...

  9. 对ORM的支持 之 8.4 集成JPA ——跟我学spring3

    8.4  集成JPA JPA全称为Java持久性API(Java Persistence API),JPA是Java EE 5标准之一,是一个ORM规范,由厂商来实现该规范,目前有Hibernate. ...

  10. C#编程(二十七)----------创建泛型类

    创建泛型类 首先介绍一个一般的,非泛型的简化链表类,可以包含任意类型的对象,以后再把这个类转化为泛型类. 在立案表中,一个元素引用下一个元素.所以必须创建一个类,他将对象封装在链表中,并引用下一个对象 ...