diff options
author | Jack Sangdahl <[email protected]> | 2025-05-20 19:04:10 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-05-20 19:04:10 -0600 |
commit | c8ddd246d9aa9ef30321642f1843e5194a7c8d7c (patch) | |
tree | eaa21f249e3ef954f089204a956f80945261c306 | |
parent | efde03c42facb93ae6dcefb0ba795463fed77f72 (diff) | |
download | leetcode-c8ddd246d9aa9ef30321642f1843e5194a7c8d7c.tar.gz leetcode-c8ddd246d9aa9ef30321642f1843e5194a7c8d7c.zip |
73. set matrix zeros
-rw-r--r-- | set_matrix_zeroes.c | 44 | ||||
-rw-r--r-- | set_matrix_zeroes.py | 34 |
2 files changed, 78 insertions, 0 deletions
diff --git a/set_matrix_zeroes.c b/set_matrix_zeroes.c new file mode 100644 index 0000000..be0e332 --- /dev/null +++ b/set_matrix_zeroes.c @@ -0,0 +1,44 @@ +// 73. Set Matrix Zeroes +#include "leetcode.h" + +/* + * given an 'm x n' integer 'matrix', if an element is '0', set its entire row + * and columns to '0's. you must do it in place + */ + +void setZeroes(int **matrix, int matrixSize, int *matrixColSize) { + bool *r = (bool *)calloc(matrixSize, sizeof(bool)); + bool *c = (bool *)calloc(*matrixColSize, sizeof(bool)); + for (int i = 0; i < matrixSize; ++i) + for (int j = 0; j < *matrixColSize; ++j) + if (!matrix[i][j]) { + r[i] = true; + r[j] = true; + } + for (int i = 0; i < matrixSize; ++i) + for (int j = 0; j < *matrixColSize; ++j) + if (r[i] || c[j]) + matrix[i][j] = 0; +} + +int main() { + int m1i[3][3] = {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}, + m2i[3][4] = {{0, 1, 2, 0}, {3, 4, 5, 2}, {1, 3, 1, 5}}; + struct two_d_arr m1, m2; + two_d_arr_init(&m1, ARRAY_SIZE(m1i), ARRAY_SIZE(m1i[0]), m1i); + two_d_arr_init(&m2, ARRAY_SIZE(m2i), ARRAY_SIZE(m2i[0]), m2i); + setZeroes(m1.arr, m1.row_size, m1.col_size); + setZeroes(m2.arr, m2.row_size, m2.col_size); + for (int i = 0; i < m1.row_size; i++) { + for (int j = 0; j < m1.col_size[i]; j++) + printf("%d ", m1i[i][j]); // expect: [[1,0,1],[0,0,0],[1,0,1]] + printf("\n"); + } + for (int i = 0; i < m2.row_size; i++) { + for (int j = 0; j < m2.col_size[i]; j++) + printf("%d ", m2i[i][j]); // expect: [[0,0,0,0],[0,4,5,0],[0,3,1,0]] + printf("\n"); + } + two_d_arr_free(&m1); + two_d_arr_free(&m2); +} diff --git a/set_matrix_zeroes.py b/set_matrix_zeroes.py new file mode 100644 index 0000000..ce47131 --- /dev/null +++ b/set_matrix_zeroes.py @@ -0,0 +1,34 @@ +# 73. Set Matrix Zeroes + +""" +given an 'm x n' integer 'matrix', if an element is '0', set its entire row +and columns to '0's. you must do it in place +""" + + +class Solution(object): + def setZeroes(self, matrix): + """ + :type matrix: List[List[int]] + :rtype: None Do not return anything, modify matrix in-place instead. + """ + r, c = 0, 0 + for i in range(len(matrix)): + for j in range(len(matrix[i])): + if matrix[i][j] == 0: + r |= 1 << i + c |= 1 << j + for i in range(len(matrix)): + for j in range(len(matrix[i])): + if r & 1 << i or c & 1 << j: + matrix[i][j] = 0 + + +if __name__ == "__main__": + obj = Solution() + m1 = [[1, 1, 1], [1, 0, 1], [1, 1, 1]] + m2 = matrix = [[0, 1, 2, 0], [3, 4, 5, 2], [1, 3, 1, 5]] + obj.setZeroes(m1) + obj.setZeroes(m2) + print(m1) + print(m2) |