Set Matrix Zeroes
Given an m x n
matrix
, return all elements of the matrix
in spiral order.
Example 1:
Example 2:
Constraints:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 10
-100 <= matrix[i][j] <= 100
Solutions
Steps
Initialize two boolean arrays
row
andcol
: These arrays are used to keep track of which rows and columns have at least one zero. The size ofrow
is the number of rows in the matrix, and the size ofcol
is the number of columns.Identify rows and columns that need to be set to zero: This is done by iterating over each element in the matrix. If an element is zero, the corresponding entries in the
row
andcol
arrays are set totrue
.Set identified rows to zero: For each
true
entry in therow
array, set all elements in that row of the matrix to zero.Set identified columns to zero: Similarly, for each
true
entry in thecol
array, set all elements in that column of the matrix to zero.
Complexity
Time complexity: O(N*M)
Space complexity: O(N+M)
Last updated