# x 73. Set Matrix Zeroes

{% embed url="<https://leetcode.com/problems/set-matrix-zeroes/description/>" %}

\---> general solution

> time: O(n x m)

> space: O(1)

```jsx
/**
 * @param {number[][]} matrix
 * @return {void} Do not return anything, modify matrix in-place instead.
 */
var setZeroes = function (matrix) {

    let firstRowZero = false;
    let firstColZero = false;

    for (let i = 0; i < matrix.length; i++) {
        for (let j = 0; j < matrix[0].length; j++) {
            const curr = matrix[i][j];
            if (curr !== 0) continue;

            if (i === 0) firstRowZero = true;
            if (j === 0) firstColZero = true;

            matrix[i][0] = 0;
            matrix[0][j] = 0;
        }
    }

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

    if (firstRowZero) {
        for (let col = 0; col < matrix[0].length; col++) {
            matrix[0][col] = 0;
        }
    }
    if (firstColZero) {
        for (let row = 0; row < matrix.length; row++) {
            matrix[row][0] = 0;
        }
    }

    return matrix;
};
```

javascript solution

> time: O(n^3)

> space: O(1)

```jsx
/**
 * @param {number[][]} matrix
 * @return {void} Do not return anything, modify matrix in-place instead.
 */
var setZeroes = function (matrix) {
    for (let i = 0; i < matrix.length; i++) {
        for (let j = 0; j < matrix[0].length; j++) {
            const curr = matrix[i][j];
            if (curr === 0) {
                for (let row = 0; row < matrix.length; row++) {
                    matrix[row][j] !== 0 && (matrix[row][j] = undefined);
                }
                for (let col = 0; col < matrix[0].length; col++) {
                    matrix[i][col] !== 0 && (matrix[i][col] = undefined);
                }
            }
        }
    }

    for (let i = 0; i < matrix.length; i++) {
        for (let j = 0; j < matrix[0].length; j++) {
            const curr = matrix[i][j];
            if (curr === undefined) {
                matrix[i][j] = 0;
            }
        }
    }

    return matrix;
};
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://algorithm.prettylog.com/top-75-leetcode-questions-to-save-your-time/problems/matrix/x-73.-set-matrix-zeroes.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
