[Leetcode] Set Matrix Zeroes

Source: Internet
Author: User

Given a m x n Matrix, if an element was 0, set its entire row and column to 0. Do it on place.

Click to show follow up.

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 is not the best solution.
Could you devise a constant space solution?

Idea: The main study is the space complexity,

Method One: Use Rowhaszero to record whether this line has 0 existence, with Colhaszero record whether this column has 0 existence. The back handles Rowhaszero and Colhaszero. Space complexity O (m+n)

classSolution { Public:        voidSetzeroes (vector<vector<int> > &matrix) {size_t row=matrix.size (); if(Row = =0)                return; size_t Col= matrix[0].size (); Vector<BOOL> Rowhaszero (Row,false); Vector<BOOL> Colhaszero (col,false);  for(inti =0; i < row; i++)            {                    for(intj =0; J < Col; J + +)                {                       if(Matrix[i][j] = =0) {Rowhaszero[i]=true; COLHASZERO[J]=true; }                   }               }                for(inti =0; i < row; i++)            {                   if(Rowhaszero[i]) { for(intj =0; J < Col; J + +) {Matrix[i][j]=0; }                   }               }                for(inti =0; I < col; i++)            {                if(Colhaszero[i]) { for(intj =0; J < Row; J + +) {Matrix[j][i]=0; }                }            }        }};

Method Two: Space complexity O (1), with the No. 0 row to record whether the column has 0, with the No. 0 column record row whether there is 0, first determine whether row 0 and column 0 is 0, and then process the matrix, if MATRIX[I][J] is 0, set matrix[i][0] = 0; MATRIX[0][J] = 0; and then according to Matrix[i][0] and Matrix[0][j] to set the corresponding row, column 0, and finally the row 0, column 0.

classSolution { Public:        voidSetzeroes (vector<vector<int> > &matrix) {size_t row=matrix.size (); if(Row = =0)                return; size_t Col= matrix[0].size (); BOOLRowzerohaszero =false; BOOLColzerohaszero =false;  for(inti =0; I < col; i++)            {                if(matrix[0][i] = =0) {Rowzerohaszero=true;  Break; }            }             for(inti =0; i < row; i++)            {                if(matrix[i][0] ==0) {Colzerohaszero=true;  Break; }            }            //cout << Rowzerohaszero << Endl; //cout << colzerohaszero << Endl;             for(inti =1; i < row; i++)            {                 for(intj =1; J < Col; J + +)                {                    if(Matrix[i][j] = =0) {matrix[i][0] =0; matrix[0][J] =0; }                }            }             for(inti =1; i < row; i++)            {                if(matrix[i][0] ==0)                {                     for(intj =0; J < Col; J + +) {Matrix[i][j]=0; }                }            }             for(inti =1; I < col; i++)            {                if(matrix[0][i] = =0)                {                     for(intj =0; J < Row; J + +) {Matrix[j][i]=0; }                }            }            //Handle the first row & first Col            if(Rowzerohaszero) { for(intj =0; J < Col; J + +) {matrix[0][J] =0; }            }            if(Colzerohaszero) { for(inti =0; i < row; i++) {matrix[i][0] =0; }            }        }};

[Leetcode] Set Matrix Zeroes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.