O(1) space c++


  • 0
    P

    Use the first line to record:

    class Solution {
    public:
        void setZeroes(vector<vector<int>>& matrix) {
            int m = matrix.size(), n = matrix[0].size(), ok = 1, ok1 = 1;
            for(int j=0;j<n&&ok;++j) if(!matrix[0][j]) ok = 0;
            for(int i=1;i<m;++i,ok1 = 1){
                for(int j=0;j<n;++j) if(!matrix[i][j]) ok1 = matrix[0][j] = 0;
                if(!ok1) matrix[i].assign(n,0);
            }
            for(int j=0;j<n;++j) if(!matrix[0][j]) for(int i=1;i<m;++i) matrix[i][j] = 0;
            if(!ok) matrix[0].assign(n,0);
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.