classSolution { public: voidsetZeroes(vector<vector<int>>& matrix){ int m = matrix.size(); int n = matrix[0].size(); vector<int> row(m), col(n); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (!matrix[i][j]) { row[i] = col[j] = true; } } } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (row[i] || col[j]) { matrix[i][j] = 0; } } } } };
publicclassSolution { publicvoidSetZeroes(int[][] matrix) { int m = matrix.Length, n = matrix[0].Length; bool[] row = newbool[m]; bool[] col = newbool[n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (matrix[i][j] == 0) { row[i] = col[j] = true; } } } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (row[i] || col[j]) { matrix[i][j] = 0; } } } } }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
var setZeroes = function(matrix) { const m = matrix.length, n = matrix[0].length; const row = newArray(m).fill(false); const col = newArray(n).fill(false); for (let i = 0; i < m; i++) { for (let j = 0; j < n; j++) { if (matrix[i][j] === 0) { row[i] = col[j] = true; } } } for (let i = 0; i < m; i++) { for (let j = 0; j < n; j++) { if (row[i] || col[j]) { matrix[i][j] = 0; } } } };
[sol1-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution: defsetZeroes(self, matrix: List[List[int]]) -> None: m, n = len(matrix), len(matrix[0]) row, col = [False] * m, [False] * n
for i inrange(m): for j inrange(n): if matrix[i][j] == 0: row[i] = col[j] = True for i inrange(m): for j inrange(n): if row[i] or col[j]: matrix[i][j] = 0
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
funcsetZeroes(matrix [][]int) { row := make([]bool, len(matrix)) col := make([]bool, len(matrix[0])) for i, r := range matrix { for j, v := range r { if v == 0 { row[i] = true col[j] = true } } } for i, r := range matrix { for j := range r { if row[i] || col[j] { r[j] = 0 } } } }
voidsetZeroes(int** matrix, int matrixSize, int* matrixColSize) { int m = matrixSize; int n = matrixColSize[0]; int row[m], col[n]; memset(row, 0, sizeof(row)); memset(col, 0, sizeof(col)); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (!matrix[i][j]) { row[i] = col[j] = true; } } } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (row[i] || col[j]) { matrix[i][j] = 0; } } } }
classSolution: defsetZeroes(self, matrix: List[List[int]]) -> None: m, n = len(matrix), len(matrix[0]) flag_col0 = any(matrix[i][0] == 0for i inrange(m)) flag_row0 = any(matrix[0][j] == 0for j inrange(n)) for i inrange(1, m): for j inrange(1, n): if matrix[i][j] == 0: matrix[i][0] = matrix[0][j] = 0 for i inrange(1, m): for j inrange(1, n): if matrix[i][0] == 0or matrix[0][j] == 0: matrix[i][j] = 0 if flag_col0: for i inrange(m): matrix[i][0] = 0 if flag_row0: for j inrange(n): matrix[0][j] = 0
funcsetZeroes(matrix [][]int) { n, m := len(matrix), len(matrix[0]) row0, col0 := false, false for _, v := range matrix[0] { if v == 0 { row0 = true break } } for _, r := range matrix { if r[0] == 0 { col0 = true break } } for i := 1; i < n; i++ { for j := 1; j < m; j++ { if matrix[i][j] == 0 { matrix[i][0] = 0 matrix[0][j] = 0 } } } for i := 1; i < n; i++ { for j := 1; j < m; j++ { if matrix[i][0] == 0 || matrix[0][j] == 0 { matrix[i][j] = 0 } } } if row0 { for j := 0; j < m; j++ { matrix[0][j] = 0 } } if col0 { for _, r := range matrix { r[0] = 0 } } }
voidsetZeroes(int** matrix, int matrixSize, int* matrixColSize) { int m = matrixSize; int n = matrixColSize[0]; int flag_col0 = false, flag_row0 = false; for (int i = 0; i < m; i++) { if (!matrix[i][0]) { flag_col0 = true; } } for (int j = 0; j < n; j++) { if (!matrix[0][j]) { flag_row0 = true; } } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { if (!matrix[i][j]) { matrix[i][0] = matrix[0][j] = 0; } } } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { if (!matrix[i][0] || !matrix[0][j]) { matrix[i][j] = 0; } } } if (flag_col0) { for (int i = 0; i < m; i++) { matrix[i][0] = 0; } } if (flag_row0) { for (int j = 0; j < n; j++) { matrix[0][j] = 0; } } }
voidsetZeroes(int** matrix, int matrixSize, int* matrixColSize) { int m = matrixSize; int n = matrixColSize[0]; int flag_col0 = false; for (int i = 0; i < m; i++) { if (!matrix[i][0]) { flag_col0 = true; } for (int j = 1; j < n; j++) { if (!matrix[i][j]) { matrix[i][0] = matrix[0][j] = 0; } } } for (int i = m - 1; i >= 0; i--) { for (int j = 1; j < n; j++) { if (!matrix[i][0] || !matrix[0][j]) { matrix[i][j] = 0; } } if (flag_col0) { matrix[i][0] = 0; } } }