vector<vector<int>> setZeroes(vector<vector<int>>& matrix) {
int rows = matrix.size(), cols = matrix[0].size();
vector<vector<int>> copy = matrix;
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (copy[r][c] != 0) continue;
for (int k = 0; k < cols; k++) matrix[r][k] = 0;
for (int k = 0; k < rows; k++) matrix[k][c] = 0;
}
}
return matrix;
}
public int[][] setZeroes(int[][] matrix) {
int rows = matrix.length, cols = matrix[0].length;
int[][] copy = new int[rows][];
for (int r = 0; r < rows; r++) copy[r] = matrix[r].clone();
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (copy[r][c] != 0) continue;
for (int k = 0; k < cols; k++) matrix[r][k] = 0;
for (int k = 0; k < rows; k++) matrix[k][c] = 0;
}
}
return matrix;
}
def set_zeroes(matrix):
rows, cols = len(matrix), len(matrix[0])
copy = [row[:] for row in matrix]
for r in range(rows):
for c in range(cols):
if copy[r][c] != 0: continue
for k in range(cols): matrix[r][k] = 0
for k in range(rows): matrix[k][c] = 0
return matrix
function setZeroes(matrix: number[][]): number[][] {
const rows = matrix.length, cols = matrix[0].length;
const copy = matrix.map((row) => [...row]);
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (copy[r][c] !== 0) continue;
for (let k = 0; k < cols; k++) matrix[r][k] = 0;
for (let k = 0; k < rows; k++) matrix[k][c] = 0;
}
}
return matrix;
}
vector<vector<int>> setZeroes(vector<vector<int>>& matrix) {
int rows = matrix.size(), cols = matrix[0].size();
vector<bool> rowZero(rows, false);
vector<bool> colZero(cols, false);
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (matrix[r][c] == 0) {
rowZero[r] = true;
colZero[c] = true;
}
}
}
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (rowZero[r] || colZero[c]) matrix[r][c] = 0;
}
}
return matrix;
}
public int[][] setZeroes(int[][] matrix) {
int rows = matrix.length, cols = matrix[0].length;
boolean[] rowZero = new boolean[rows];
boolean[] colZero = new boolean[cols];
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (matrix[r][c] == 0) {
rowZero[r] = true;
colZero[c] = true;
}
}
}
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (rowZero[r] || colZero[c]) matrix[r][c] = 0;
}
}
return matrix;
}
def set_zeroes(matrix):
rows, cols = len(matrix), len(matrix[0])
row_zero = [False] * rows
col_zero = [False] * cols
for r in range(rows):
for c in range(cols):
if matrix[r][c] == 0:
row_zero[r] = True
col_zero[c] = True
for r in range(rows):
for c in range(cols):
if row_zero[r] or col_zero[c]: matrix[r][c] = 0
return matrix
function setZeroes(matrix: number[][]): number[][] {
const rows = matrix.length, cols = matrix[0].length;
const rowZero = new Array<boolean>(rows).fill(false);
const colZero = new Array<boolean>(cols).fill(false);
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (matrix[r][c] === 0) {
rowZero[r] = true;
colZero[c] = true;
}
}
}
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (rowZero[r] || colZero[c]) matrix[r][c] = 0;
}
}
return matrix;
}
vector<vector<int>> setZeroes(vector<vector<int>>& matrix) {
int rows = matrix.size(), cols = matrix[0].size();
bool firstRowZero = false, firstColZero = false;
for (int c = 0; c < cols; c++) if (matrix[0][c] == 0) firstRowZero = true;
for (int r = 0; r < rows; r++) if (matrix[r][0] == 0) firstColZero = true;
for (int r = 1; r < rows; r++) {
for (int c = 1; c < cols; c++) {
if (matrix[r][c] == 0) {
matrix[r][0] = 0;
matrix[0][c] = 0;
}
}
}
for (int r = 1; r < rows; r++) {
for (int c = 1; c < cols; c++) {
if (matrix[r][0] == 0 || matrix[0][c] == 0) matrix[r][c] = 0;
}
}
if (firstRowZero) for (int c = 0; c < cols; c++) matrix[0][c] = 0;
if (firstColZero) for (int r = 0; r < rows; r++) matrix[r][0] = 0;
return matrix;
}
public int[][] setZeroes(int[][] matrix) {
int rows = matrix.length, cols = matrix[0].length;
boolean firstRowZero = false, firstColZero = false;
for (int c = 0; c < cols; c++) if (matrix[0][c] == 0) firstRowZero = true;
for (int r = 0; r < rows; r++) if (matrix[r][0] == 0) firstColZero = true;
for (int r = 1; r < rows; r++) {
for (int c = 1; c < cols; c++) {
if (matrix[r][c] == 0) {
matrix[r][0] = 0;
matrix[0][c] = 0;
}
}
}
for (int r = 1; r < rows; r++) {
for (int c = 1; c < cols; c++) {
if (matrix[r][0] == 0 || matrix[0][c] == 0) matrix[r][c] = 0;
}
}
if (firstRowZero) for (int c = 0; c < cols; c++) matrix[0][c] = 0;
if (firstColZero) for (int r = 0; r < rows; r++) matrix[r][0] = 0;
return matrix;
}
def set_zeroes(matrix):
rows, cols = len(matrix), len(matrix[0])
first_row_zero = first_col_zero = False
for c in range(cols):
if matrix[0][c] == 0: first_row_zero = True
for r in range(rows):
if matrix[r][0] == 0: first_col_zero = True
for r in range(1, rows):
for c in range(1, cols):
if matrix[r][c] == 0:
matrix[r][0] = 0
matrix[0][c] = 0
for r in range(1, rows):
for c in range(1, cols):
if matrix[r][0] == 0 or matrix[0][c] == 0: matrix[r][c] = 0
if first_row_zero:
for c in range(cols): matrix[0][c] = 0
if first_col_zero:
for r in range(rows): matrix[r][0] = 0
return matrix
function setZeroes(matrix: number[][]): number[][] {
const rows = matrix.length, cols = matrix[0].length;
let firstRowZero = false, firstColZero = false;
for (let c = 0; c < cols; c++) if (matrix[0][c] === 0) firstRowZero = true;
for (let r = 0; r < rows; r++) if (matrix[r][0] === 0) firstColZero = true;
for (let r = 1; r < rows; r++) {
for (let c = 1; c < cols; c++) {
if (matrix[r][c] === 0) {
matrix[r][0] = 0;
matrix[0][c] = 0;
}
}
}
for (let r = 1; r < rows; r++) {
for (let c = 1; c < cols; c++) {
if (matrix[r][0] === 0 || matrix[0][c] === 0) matrix[r][c] = 0;
}
}
if (firstRowZero) for (let c = 0; c < cols; c++) matrix[0][c] = 0;
if (firstColZero) for (let r = 0; r < rows; r++) matrix[r][0] = 0;
return matrix;
}
vector<vector<int>> setZeroes(vector<vector<int>>& matrix) {
int rows = matrix.size(), cols = matrix[0].size();
bool col0 = false;
for (int r = 0; r < rows; r++) {
if (matrix[r][0] == 0) col0 = true;
for (int c = 1; c < cols; c++) {
if (matrix[r][c] == 0) {
matrix[r][0] = 0;
matrix[0][c] = 0;
}
}
}
for (int r = rows - 1; r >= 0; r--) {
for (int c = cols - 1; c >= 1; c--) {
if (matrix[r][0] == 0 || matrix[0][c] == 0) matrix[r][c] = 0;
}
if (col0) matrix[r][0] = 0;
}
return matrix;
}
public int[][] setZeroes(int[][] matrix) {
int rows = matrix.length, cols = matrix[0].length;
boolean col0 = false;
for (int r = 0; r < rows; r++) {
if (matrix[r][0] == 0) col0 = true;
for (int c = 1; c < cols; c++) {
if (matrix[r][c] == 0) {
matrix[r][0] = 0;
matrix[0][c] = 0;
}
}
}
for (int r = rows - 1; r >= 0; r--) {
for (int c = cols - 1; c >= 1; c--) {
if (matrix[r][0] == 0 || matrix[0][c] == 0) matrix[r][c] = 0;
}
if (col0) matrix[r][0] = 0;
}
return matrix;
}
def set_zeroes(matrix):
rows, cols = len(matrix), len(matrix[0])
col0 = False
for r in range(rows):
if matrix[r][0] == 0: col0 = True
for c in range(1, cols):
if matrix[r][c] == 0:
matrix[r][0] = 0
matrix[0][c] = 0
for r in range(rows - 1, -1, -1):
for c in range(cols - 1, 0, -1):
if matrix[r][0] == 0 or matrix[0][c] == 0: matrix[r][c] = 0
if col0: matrix[r][0] = 0
return matrix
function setZeroes(matrix: number[][]): number[][] {
const rows = matrix.length, cols = matrix[0].length;
let col0 = false;
for (let r = 0; r < rows; r++) {
if (matrix[r][0] === 0) col0 = true;
for (let c = 1; c < cols; c++) {
if (matrix[r][c] === 0) {
matrix[r][0] = 0;
matrix[0][c] = 0;
}
}
}
for (let r = rows - 1; r >= 0; r--) {
for (let c = cols - 1; c >= 1; c--) {
if (matrix[r][0] === 0 || matrix[0][c] === 0) matrix[r][c] = 0;
}
if (col0) matrix[r][0] = 0;
}
return matrix;
}