leetcode73 matrix zero

 

 https://leetcode-cn.com/problems/set-matrix-zeroes/

answer:

Both methods are time complexity is O (mn)

O (m + n) space Method:

0 stored as rows and columns with two containers

class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        //O(m+n)额外空间,常数空间???
        set<int> rse,cse;
        int r=matrix.size();int c=matrix[0].size();
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                if(matrix[i][j]==0){
                    rse.insert(i);
                    cse.insert(j);
                }
            }
        }
        while(!rse.empty()){
            int temp=*rse.begin();
            for(int j=0;j<c;j++){
                matrix[temp][j]=0;
            }
            rse.erase(rse.begin());
        }
        while(!cse.empty()){
            int temp=*cse.begin();
            for(int i=0;i<r;i++){
                matrix[i][temp]=0;
            } 
            Cse.erase (cse.begin ()); 
        } 
        
        
    } 
};

 

Constant-space method:

And the data for the first row 0 column 0 if 0, the flag isrow = true, iscol = true is recorded is 0;

Data for m rows and columns 1 ~ 1 ~ n if 0, it is marked in row 0, column 0; i.e.

i : 1~m-1
    j:  1~n-1
        if(matrix[i][j]==0)
            matrix[i][0]=0,matrix[0][j]=0; 

 

C++ code:

class Solution {
 public :
     void setZeroes (Vector <Vector < int >> & Matrix) {
         // constant spatial solutions 
        
        // flag 0th row and 0th column is whether 0; 
        BOOL IsRow = to false ;
         BOOL iscol = to false ;
         int = R & lt matrix.size ();
         int C = Matrix [ 0 ] .size (); 
        
        for ( int J = 0 ; J <C; J ++ ) {
             IF (Matrix [ 0 ] [J] == 0 ) { 
                IsRow =to true ; BREAK ; 
            } 
        } 
        for ( int I = 0 ; I <R & lt; I ++ ) {
             IF (Matrix [I] [ 0 ] == 0 ) { 
                iscol = to true ; BREAK ; 
            } 
        } 
        
        // flag column is 1 ~ n is 0, the result placed in the 0th row and 0th column; 
        for ( int I = . 1 ; I <R & lt; I ++ ) {
             for ( int J = . 1 ; J <C; J ++ ) {
                 IF (Matrix [I] [ j] ==0 ) { 
                    Matrix [I] [ 0 ] = 0 ; 
                    Matrix [ 0 ] [J] = 0 ; 
                } 
            } 
        } 
        
        // First alternative ranks 1 ~ n values 
        for ( int I = . 1 ; I <R & lt; I ++ ) {
             IF (Matrix [I] [ 0 ] == 0 ) {
                 for ( int J = 0 ; J <C; J ++ ) { 
                    Matrix [I] [J] = 0 ; 
                }  
            }
        }
        for(int j=1;j<c;j++){
            if(matrix[0][j]==0){
                for(int i=0;i<r;i++){
                    matrix[i][j]=0;
                }
            }
        }
        //再替换0行和0列
        if(isrow){
            for(int j=0;j<c;j++){
                matrix[0][j]=0;
            }
        }
        if(iscol){
            for(int i=0;i<r;i++){
                matrix[i][0]=0;
            }
        }
    }
};

 

Guess you like

Origin www.cnblogs.com/joelwang/p/11896781.html