LeetCode -- 73. Set Matrix Zeroes

 

//O(1) MODIFIED = INT_MIN+1; This is a trick value.
class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        int MODIFIED = INT_MIN+1;
        int R = matrix.size();
        int C = matrix[0].size();
        for(int r=0; r<R; r++){
            for(int c=0; c<C; c++){
                if(matrix[r][c] == 0){
                    //mark all row:r col:c to MODIFIED
                    for(int cc=0; cc<C; cc++){ //rows
                        if(matrix[r][cc] != 0)
                            matrix[r][cc] = MODIFIED;
                    }                    
                    for(int rr=0; rr<R; rr++){ //cols
                        if(matrix[rr][c] != 0)
                            matrix[rr][c] = MODIFIED;
                    }
                }
            }
        }
        for(int r=0; r<R; r++){
            for(int c=0; c<C; c++){
                if(matrix[r][c] == MODIFIED)
                    matrix[r][c] = 0;
            }
        }
    }
};

/* vector search slow
class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        vector<int> rows, cols;
        int rowlen = matrix.size(), collen= matrix[0].size();
        for(int i=0; i<rowlen; i++){
            for(int j=0; j<collen; j++){
                if(matrix[i][j] == 0){
                    rows.push_back(i);
                    cols.push_back(j);
                }
            }
        }
        
        for(int i=0; i<rowlen; i++){
            for(int j=0; j<collen; j++){
                if(rows.end()!=find(rows.begin(), rows.end(), i) || cols.end() != find(cols.begin(), cols.end(), j))
                    matrix[i][j] = 0;
            }
        }
    }
};
*/

  

 

class Solution {
  public void setZeroes(int[][] matrix) {
    Boolean isCol = false;
    int R = matrix.length;
    int C = matrix[0].length;

    for (int i = 0; i < R; i++) {

      // Since first cell for both first row and first column is the same i.e. matrix[0][0]
      // We can use an additional variable for either the first row/column.
      // For this solution we are using an additional variable for the first column
      // and using matrix[0][0] for the first row.
      if (matrix[i][0] == 0) {
        isCol = true;
      }

      for (int j = 1; j < C; j++) {
        // If an element is zero, we set the first element of the corresponding row and column to 0
        if (matrix[i][j] == 0) {
          matrix[0][j] = 0;
          matrix[i][0] = 0;
        }
      }
    }

    // Iterate over the array once again and using the first row and first column, update the elements.
    for (int i = 1; i < R; i++) {
      for (int j = 1; j < C; j++) {
        if (matrix[i][0] == 0 || matrix[0][j] == 0) {
          matrix[i][j] = 0;
        }
      }
    }

    // See if the first row needs to be set to zero as well
    if (matrix[0][0] == 0) {
      for (int j = 0; j < C; j++) {
        matrix[0][j] = 0;
      }
    }

    // See if the first column needs to be set to zero as well
    if (isCol) {
      for (int i = 0; i < R; i++) {
        matrix[i][0] = 0;
      }
    }
  }

  

void setZeroes( int** matrix, int matrixSize, int* matrixColSize ) {
    bool setfirstRowToZero = false;
    bool setfirstColToZero = false;


    for ( int i = 0; i < matrixSize; i++ )
    {
        for ( int j = 0; j < matrixColSize[0]; j++ )
        {
            if ( matrix[i][j] == 0 )
            {
                if ( i == 0 )
                {
                    setfirstRowToZero = true;
                }
                if ( j == 0 )
                {
                    setfirstColToZero = true;
                }
                matrix[i][0] = 0;
                matrix[0][j] = 0;
            }
        }
    }

    for ( int i = 1; i < matrixSize; i++ )
    {
        if ( matrix[i][0] == 0 )
        {
            for ( int j = 0; j < matrixColSize[0]; j++ )
            {
                matrix[i][j] = 0;
            }
        }
    }

    for ( int j = 1; j < matrixColSize[0]; j++ )
    {
        if ( matrix[0][j] == 0 )
        {
            for ( int i = 0; i < matrixSize; i++ )
            {
                matrix[i][j] = 0;
            }
        }
    }

    if ( setfirstColToZero == true )
    {
        for ( int i = 1; i < matrixSize; i++ )
        {
            matrix[i][0] = 0;
        }
    }
    if( setfirstRowToZero == true)
    {
        for ( int j = 1; j < matrixColSize[0]; j++ )
        {
            matrix[0][j] = 0;
        }
    }

}

  

posted on 2019-06-20 16:17  Felizño  阅读(259)  评论(0编辑  收藏  举报

导航