[Leedcode 73] 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.

 Public classSolution { Public voidSetzeroes (int[] matrix) {        //The topic requires no extra space, so you need to use the space of the matrix itself to assist storage,//The first and first columns of the matrix are borrowed to assist in recording the row or whether the column is 0. Because the first column of the first row itself has changed, and then two variables are used to record whether the first row and the first column are//0.         intrlen=matrix.length; intClen=matrix[0].length; intRow=1; intCol=1;  for(inti=0;i<rlen;i++){            if(matrix[i][0]==0) {col=0;  Break; }        }         for(inti=0;i<clen;i++){            if(matrix[0][i]==0) {row=0;  Break; }        }         for(inti=1;i<rlen;i++) {//Note: No special considerations are required for rows 1th and 1th, as only the original value can be guaranteed!!              for(intj=1;j<clen;j++){                    if(matrix[i][j]==0) {matrix[i][0]=0; matrix[0][j]=0; }            }        }         for(inti=1;i<rlen;i++){             for(intj=1;j<clen;j++){                if(matrix[i][0]==0| | Matrix[0][j]==0) Matrix[i][j]=0; }        }        if(row==0){             for(inti=0;i<clen;i++) {matrix[0][i]=0; }                    }        if(col==0){             for(inti=0;i<rlen;i++) {matrix[i][0]=0; }        }                    }}

[Leedcode 73] 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.