73. Set Matrix Zeroes #1709
-
73. Set Matrix Zeroes Difficulty: Medium Topics: Given an You must do it in place. Example 1:
Example 2:
Constraints:
Follow up:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to set the entire row and column of any element in a matrix to zero if that element is zero. The challenge is to achieve this in-place with constant space complexity. ApproachThe key idea is to use the first row and first column of the matrix to keep track of which rows and columns need to be zeroed. This allows us to avoid using additional space for storing this information. Here's the step-by-step approach:
Let's implement this solution in PHP: 73. Set Matrix Zeroes <?php
/**
* @param Integer[][] $matrix
* @return NULL
*/
function setZeroes(&$matrix) {
$m = count($matrix);
if ($m == 0) return;
$n = count($matrix[0]);
$firstRowZero = false;
$firstColZero = false;
// Check if first row has any zero
for ($j = 0; $j < $n; $j++) {
if ($matrix[0][$j] == 0) {
$firstRowZero = true;
break;
}
}
// Check if first column has any zero
for ($i = 0; $i < $m; $i++) {
if ($matrix[$i][0] == 0) {
$firstColZero = true;
break;
}
}
// Mark rows and columns based on other elements
for ($i = 1; $i < $m; $i++) {
for ($j = 1; $j < $n; $j++) {
if ($matrix[$i][$j] == 0) {
$matrix[$i][0] = 0;
$matrix[0][$j] = 0;
}
}
}
// Set rows to zero based on first column
for ($i = 1; $i < $m; $i++) {
if ($matrix[$i][0] == 0) {
for ($j = 0; $j < $n; $j++) {
$matrix[$i][$j] = 0;
}
}
}
// Set columns to zero based on first row
for ($j = 1; $j < $n; $j++) {
if ($matrix[0][$j] == 0) {
for ($i = 0; $i < $m; $i++) {
$matrix[$i][$j] = 0;
}
}
}
// Set first row to zero if needed
if ($firstRowZero) {
for ($j = 0; $j < $n; $j++) {
$matrix[0][$j] = 0;
}
}
// Set first column to zero if needed
if ($firstColZero) {
for ($i = 0; $i < $m; $i++) {
$matrix[$i][0] = 0;
}
}
}
// Example usage:
print_r setZeroes([[1,1,1],[1,0,1],[1,1,1]]) . "\n"; // Output: [[1,0,1],[0,0,0],[1,0,1]]
print_r setZeroes([[0,1,2,0],[3,4,5,2],[1,3,1,5]]) . "\n"; // Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
?> Explanation:
This approach efficiently uses the matrix itself to track necessary information, achieving the goal with constant space complexity. |
Beta Was this translation helpful? Give feedback.
We need to set the entire row and column of any element in a matrix to zero if that element is zero. The challenge is to achieve this in-place with constant space complexity.
Approach
The key idea is to use the first row and first column of the matrix to keep track of which rows and columns need to be zeroed. This allows us to avoid using additional space for storing this information. Here's the step-by-step approach:
Check First Row and Column for Zeros: Determine if the first row or column originally contains any zeros. This information is stored in boolean variables (
firstRowZero
andfirstColZero
).Mark Rows and Columns: Iterate through the matrix starting from the second row and s…