题目

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. 修改无线wifi网络名称。注册表。windows 无线属性 windows 无线 配置文件

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha windows 无线属性 windows 无线 配置文件 ======= 修改完成,之后 ...

  2. [Java]进程与线程的区别(转)

    线程是指进程内的一个执行单元,也是进程内的可调度实体. 与进程的区别: (1)地址空间:进程内的一个执行单元;进程至少有一个线程;它们共享进程的地址空间;而进程有自己独立的地址空间; (2)资源拥有: ...

  3. 【原】Spring整合Redis(第二篇)—SDR环境搭建具体步骤

    [环境参数] Spring版本:4.2.6.RELEASESpring-Data-Redis版本:1.7.2.RELEASE Redis版本:redis-2.4.5-win32-win64 [简要说明 ...

  4. IntraWeb XIV 类型速查表

    tkClass ================== IWUserSessionBase.TIWUserSessionBase < TDataModule < TComponent < ...

  5. 鸟哥的Linux私房菜 基础学习篇读书笔记(9):Linux磁盘与文件系统管理(2)

    上一篇文章主要从理论上分析了Linux的Ext2文件系统.这一篇主要解说怎样查看Linux的文件系统的容量以及解说Linux文件系统中的连接文件. 能够通过df和du命令来查看磁盘与文件夹的容量.df ...

  6. HAL驱动的串口编程陷阱

    http://bbs.elecfans.com/jishu_464356_1_1.html 手上有块NUCLEO STM32L053x板子,用来做串口实验,看了下ST的最新库HAL驱动,于是想用HAL ...

  7. 轮子科技的.NET Core分享

    2016年8月11日 应轮子科技一众好友的邀请,在轮子科技给大家做了一个无责任的瞎聊段落,聊聊.NET的Core的一些内容. 恩,演讲者就只有我一个了,讲师是微软的 MVP 杨守斌,就是因为这个,所以 ...

  8. ExtJS动态设置表头

    if(document.getElementById("lxdj_radio").checked){ colQd = new Ext.grid.ColumnModel(colMAr ...

  9. 【Centos】centos查看磁盘使用情况

    1.查看分区和磁盘 lsblk 查看分区和磁盘 2.查看空间使用情况 df -h 查看空间使用情况 3.分区工具查看分区信息 fdisk -l 分区工具查看分区信息 4.查看分区 cfdisk /de ...

  10. SurfaceFlinger( 226): Permission Denial: can't access SurfaceFlinger

    MODIFY_PHONE_STATE permission is granted to system apps only. For your information, there are 2 type ...