题目

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. BZOJ.2440.[中山市选2011]完全平方数(莫比乌斯函数 二分)

    题目链接 总感觉博客园的\(Markdown\)很..\(gouzhi\),可以看这的. 题意即求第\(k\)个无平方因子数. 无平方因子数(Square-Free Number),即分解之后所有质因 ...

  2. BZOJ.2246.[SDOI2011]迷宫探险(DP 记忆化搜索 概率)

    题目链接 求最大的存活概率,DP+记忆化. 用f[s][x][y][hp]表示在s状态,(x,y)点,血量为hp时的存活概率. s是个三进制数,记录每个陷阱无害/有害/未知. 转移时比较容易,主要是在 ...

  3. iOS键盘类型最全

    一.键盘风格 UIKit框架支持8种风格键盘. typedef enum { UIKeyboardTypeDefault,                // 默认键盘:支持所有字符 UIKeyboa ...

  4. CAP原则(CAP定理)、BASE理论

    CAP原则又称CAP定理,指的是在一个分布式系统中, Consistency(一致性). Availability(可用性).Partition tolerance(分区容错性),三者不可得兼. CA ...

  5. 跟着老王学Python

    亲爱的朋友:     欢迎你!很高兴能在这里见到你,你能来到这里说明你真的很喜欢python,很想把python给学好!我觉的你很幸运,开始我学python的时候比较少资料,学起来也比较头疼,现在随着 ...

  6. resteasy经验谈

    resteasy 是java体系中比较成熟的rest框架,也是jax-rs规范的实现之一,dubbox的REST服务框架,就是采用的resteasy实现,近日在实际项目中遇到了几个问题,记录于此:   ...

  7. C++11 function

    #include <iostream> #include <functional> #include <vector> using namespace std; / ...

  8. Snmp学习总结系列——开篇

    进入公司以来,一直参与到公司的产品研发工作当中去,在产品研发中有一个监控远程服务器CPU使用率,内存使用情况,硬盘的需求,技术总监提出了使用Snmp协议作为远程监控的技术解决方案,头一次听说Snmp这 ...

  9. iOS webservice 接口使用方法

    1.没有参数的webservice 接口,如下: 接口名:获取服务器时间 方法名: getServerTime() 参数:空 返回值:服务器当前时间: yyyy/MM/dd HH:mm:ss 使用方法 ...

  10. windows7文件共享 详细步骤 图解

    windows7文件共享详细步骤图解 http://wenku.baidu.com/link?url=PROjBfZ0KZFiAlgpb6sdaZBnSzFNTx3Ui_YXs5-IVoKBf2pPN ...