← All problems

Set Matrix Zeroes (O(1) space)

Medium matrixarrays LeetCode ↗
Given an m x n matrix, if any cell is zero, set its entire row and column to zero — using only constant extra space. The first row/column can serve as marker storage with care.

Hints (0/3 revealed)

Log in to reveal hints progressively.

Complexity

Time O(mn), Space O(1)

Common pitfalls

  • Zeroing markers prematurely
  • Forgetting the two flags

Your solution

Log in to save your code and track progress.