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
  • 相关阅读:
    mina 字节数组编解码器的写法 I
    latex编写论文
    HTML学习
    Apache Mina Filter
    静态循环队列的相关操作及详解
    hdu1242 Rescue(BFS +优先队列 or BFS )
    让我们区分质量保证与测试
    新玩的windows phone app studio
    Pylons安装苦逼之路
    [置顶] js 实现 <input type="file" /> 文件上传
  • 原文地址:https://www.cnblogs.com/freeneng/p/3099935.html
Copyright © 2011-2022 走看看