• 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.

    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?

     
    [解题思路]
    1.空间复杂度为:O(m+n)
     1 public void setZeroes(int[][] matrix) {
     2         // Start typing your Java solution below
     3         // DO NOT write main() function
     4         if(matrix.length == 0){
     5             return;
     6         }
     7         int r = matrix.length;
     8         int c = matrix[0].length;
     9         int[] rows = new int[r];
    10         for(int i = 0; i < r; i++){
    11             rows[i] = 1;
    12         }
    13         int[] cols = new int[c];
    14         for(int i = 0; i < c; i++){
    15             cols[i] = 1;
    16         }
    17         for(int i = 0; i < r; i++){
    18             for(int j = 0; j < c; j++){
    19                 if(matrix[i][j] == 0){
    20                     rows[i] = 0;
    21                     cols[j] = 0;
    22                 }
    23             }
    24         }
    25         
    26         for(int i = 0; i < r; i++){
    27             if(rows[i] == 0){
    28                 for(int j = 0; j < c; j++){
    29                     matrix[i][j] = 0;
    30                 }
    31             }
    32         }
    33         
    34         for(int i = 0; i < c; i++){
    35             if(cols[i] == 0){
    36                 for(int j = 0; j < r; j++){
    37                     matrix[j][i] = 0;
    38                 }
    39             }
    40         }
    41     }

     2.constant space solution

    常数空间,第一可以考虑是不是固定数量的几个变量能解决,否则可以考虑是不是问题本身已经提供了足够的空间

    本题属于后者,利用矩阵的第一行和第一列来作辅助空间使用

    1.先确定第一行和第一列是否需要清零
    即,看看第一行中是否有0,记下来。也同时记下来第一列中有没有0。
    2.扫描剩下的矩阵元素,如果遇到了0,就将对应的第一行和第一列上的元素赋值为0
    这里不用担心会将本来第一行或第一列的1改成了0,因为这些值最后注定要成为0的。
    3.根据第一行和第一列的信息,已经可以将剩下的矩阵元素赋值为结果所需的值了
    即,拿第一行为例,如果扫描到一个0,就将这一列都清0.
    4.根据1中确定的状态,处理第一行和第一列。
    如果最开始得到的第一行中有0的话,就整行清零。同理对列进行处理。

     1 public void setZeroes(int[][] matrix) {
     2         // Start typing your Java solution below
     3         // DO NOT write main() function
     4         if(matrix.length == 0){
     5             return;
     6         }
     7         int r = matrix.length;
     8         int c = matrix[0].length;
     9         
    10         boolean zeroRow = false, zeroCol = false;
    11         for(int i = 0; i < c; i++){
    12             if(matrix[0][i] == 0){
    13                 zeroRow = true;
    14                 break;
    15             }
    16         }
    17         for(int i = 0; i < r; i++){
    18             if(matrix[i][0] == 0){
    19                 zeroCol = true;
    20                 break;
    21             }
    22         }
    23         
    24         for(int i = 1; i < r; i++){
    25             for(int j = 1; j < c; j++){
    26                 if(matrix[i][j] == 0){
    27                     matrix[i][0] = 0;
    28                     matrix[0][j] = 0;
    29                 }
    30             }
    31         }
    32         
    33         for(int i = 1; i < r;i ++){
    34             for(int j = 1; j < c; j++){
    35                 if(matrix[i][0] == 0 || matrix[0][j] == 0){
    36                     matrix[i][j] = 0;
    37                 }
    38             }
    39         }
    40         
    41         if(zeroRow){
    42             for(int i = 0; i < c; i++){
    43                 matrix[0][i] = 0;
    44             }
    45         }
    46         
    47         if(zeroCol){
    48             for(int i = 0; i < r; i++){
    49                 matrix[i][0] = 0;
    50             }
    51         }
    52     }
  • 相关阅读:
    B1295 [SCOI2009]最长距离 最短路
    B1588 [HNOI2002]营业额统计 set||平衡树
    B1202 [HNOI2005]狡猾的商人 并查集
    B1303 [CQOI2009] 中位数图 数学
    B2002 [Hnoi2010]Bounce 弹飞绵羊 分块
    B1192 [HNOI2006]超级英雄Hero 二分图匹配
    逐个击破
    HAOI2009 毛毛虫
    HNOI/AHOI2018 道路
    NOI2005 瑰丽华尔兹
  • 原文地址:https://www.cnblogs.com/feiling/p/3269704.html
Copyright © 2020-2023  润新知