clueless coding // TODO: be smarter

LeetCode Set Matrix Zeroes: CPP Solution



TL;DR Code Solution


void setZeroes(vector<vector<int>>& matrix) {

    bool rowZero = false;
    bool colZero = false;

    for (int i = 0; i < matrix.size(); i++) {
        for (int j = 0; j < matrix[0].size(); j++) {
            if (matrix[i][j] == 0) {
                if (i == 0) rowZero = true;
                if (j == 0) colZero = true;
                matrix[0][j] = 0;
                matrix[i][0] = 0;
            }
        }
    }

    for (int i = 1; i < matrix.size(); i++) {
        for (int j = 1; j < matrix[0].size(); j++) {
            if (matrix[0][j] == 0 || matrix[i][0] == 0) {
                matrix[i][j] = 0;
            }
        }
    }

    if (rowZero) {
        for (int i = 0; i < matrix[0].size(); i++) {
            matrix[0][i] = 0;
        }
    }
    if (colZero) {
        for (int i = 0; i < matrix.size(); i++) {
            matrix[i][0] = 0;
        }
    }

}