> For the complete documentation index, see [llms.txt](https://algorithm.prettylog.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://algorithm.prettylog.com/top-75-leetcode-questions-to-save-your-time/problems/matrix/x-73.-set-matrix-zeroes.md).

# 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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` 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>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
