• LeetCode 笔记系列15 Set Matrix Zeroes [稍微有一点hack]


    题目:Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

    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?

    这个问题使用额外的空间来完成比较直观,但是人要求用“CONSTANT SPACE” 。。。

    我想了半天也没想出来为啥,只有看看leetcode的讨论了。http://discuss.leetcode.com/questions/249/set-matrix-zeroes

    其实也不是很难,稍微有点hack。就是首先用两个bool变量记录下第一行和第一列里面是不是有0。接下来利用第一行和第一列保存那些位于[1~m,1~n]范围内的元素的0的位置。就是,如果(i,j)是0,那么我们设置(i,0)(0,j)为0. 所以其实第一行和第一列有点纵横坐标的意思,这样表示待会儿我们要把第i行和第j列都设置成0

    然后再次遍历[1~m,1~n],如果(i,0)或者(0,j)是0,那么(i,j)为0。注意到这次遍历的时候,那些本身在0行或者0列的0(图中的黄色)也造成了所在列或者行的设置。

    最后,根据前面两个bool变量的值,设置第一行和第一列。注意到这个时候我们只设置行或者列,因为如果某行之前有0,所在的列在上一步中已经设置过了。

    代码如下:

     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?
    10     for (int j = 0; j < colnum; ++j) {
    11         if (matrix[0][j] == 0) {
    12             hasZeroFirstRow = true;
    13             break;
    14         }
    15     }
    16 
    17     // Does first column have zero?
    18     for (int i = 0; i < rownum; ++i) {
    19         if (matrix[i][0] == 0) {
    20             hasZeroFirstColumn = true;
    21             break;
    22         }
    23     }
    24 
    25     // find zeroes and store the info in first row and column
    26     for (int i = 1; i < matrix.length; ++i) {
    27         for (int j = 1; j < matrix[0].length; ++j) {
    28             if (matrix[i][j] == 0) {
    29                 matrix[i][0] = 0;
    30                 matrix[0][j] = 0;
    31             }
    32         }
    33     }
    34 
    35     // set zeroes except the first row and column
    36     for (int i = 1; i < matrix.length; ++i) {
    37         for (int j = 1; j < matrix[0].length; ++j) {
    38             if (matrix[i][0] == 0 || matrix[0][j] == 0)  matrix[i][j] = 0;
    39         }
    40     }
    41 
    42     // set zeroes for first row and column if needed
    43     if (hasZeroFirstRow) {
    44         for (int j = 0; j < colnum; ++j) {
    45             matrix[0][j] = 0;
    46         }
    47     }
    48     if (hasZeroFirstColumn) {
    49         for (int i = 0; i < rownum; ++i) {
    50             matrix[i][0] = 0;
    51         }
    52     }
    53 }
    View Code

    总结下。

    这个方法并不是太容易想到。是个算比较hack的方法。当然也算一个技巧了。

  • 相关阅读:
    Web 应用程序中的安全向量 – ASP.NET MVC 4 系列
    成员资格、授权 – ASP.NET MVC 4 系列
    数据注解和验证 – ASP.NET MVC 4 系列
    表单和 HTML 辅助方法– ASP.NET MVC 4 系列
    模型(Model)– ASP.NET MVC 4 系列
    Razor 视图引擎 – ASP.NET MVC 4 系列
    视图(View) – ASP.NET MVC 4 系列
    控制器(Controller) – ASP.NET MVC 4 系列
    简介 – ASP.NET MVC 4 系列
    一般处理程序处理图片(动态给图片加上水印、保存缩略图、验证码)
  • 原文地址:https://www.cnblogs.com/lichen782/p/leetcode_Set_Matrtix_Zero.html
Copyright © 2020-2023  润新知