zoukankan      html  css  js  c++  java
  • [Leetcode 61] 73 Set Matrix Zeros

    Problem:

    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?

    Analysis:

    The simplest way is to have a copy of the given matrix and then find 0's indexes in the aux matrix and set the rows and columns to 0 in original matrix. This requires sapce O(mn)

    Next method is the scan through the matrix, keep record of rows and columns that need to be set to 0. This requires space O(m+n)

    After referencing the online resource, find the following constant space solution.

    To use constant space to solve a problem, either 1). use constant variables to solve the problem or 2). make use of the current space to solve the problem. In this problem is the second case.

    1. scan the first row and first column to remember whether need to set them to zero.

    2. scan the rest part of the matrix, if a position (x, y) is zero. Set (x, 0) and (0, y) to zero.

    3. scan again the first row and first column to set rows and columns to 0s.

    4. process the first row and first column according to step 1.

    Code:

     1 class Solution {
     2 public:
     3     void setZeroes(vector<vector<int> > &matrix) {
     4         // Start typing your C/C++ solution below
     5         // DO NOT write int main() function
     6         int m = matrix.size();
     7         int n = matrix[0].size();
     8         bool setFirstRow = false, setFirstCol = false;
     9         
    10         for (int i=0; i<n; i++)
    11             if (matrix[0][i] == 0) {
    12                 setFirstRow = true;
    13                 break;
    14             }
    15             
    16         for (int i=0; i<m; i++)
    17             if (matrix[i][0] == 0) {
    18                 setFirstCol = true;
    19                 break;
    20             }
    21         
    22         for (int i=1; i<m; i++)
    23             for (int j=1; j<n; j++) {
    24                 if (matrix[i][j] == 0) {
    25                     matrix[0][j] = 0;
    26                     matrix[i][0] = 0;
    27                 }
    28             }
    29             
    30         for (int i=1; i<n; i++) {
    31             if (matrix[0][i] == 0) {
    32                 for (int j=1; j<m; j++) 
    33                     matrix[j][i] = 0;
    34             }
    35         }    
    36         
    37         
    38         for (int i=1; i<m; i++) {
    39             if (matrix[i][0] == 0) {
    40                 for (int j=1; j<n; j++)
    41                     matrix[i][j] = 0;
    42             }
    43         }
    44         
    45         if (setFirstRow) {
    46             for (int i=0; i<n; i++)
    47                 matrix[0][i] = 0;
    48         }
    49         
    50         if (setFirstCol) {
    51             for (int i=0; i<m; i++)
    52                 matrix[i][0] = 0;
    53         }
    54     }
    55 }; 
    View Code
  • 相关阅读:
    列表去重
    URL和URI的不同
    functional program language
    thinkphp5_笔记二
    关于《提问智慧》的笔记
    实习记录_2
    关于用户表的设计
    30秒运行超时的错误(Maximum execution time of 30 seconds exceeded)
    thinkphp5_笔记一
    Jquary 和Ajax实现简单的异步请求
  • 原文地址:https://www.cnblogs.com/freeneng/p/3099935.html
Copyright © 2011-2022 走看看