File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * <a href="https://leetcode.com/problems/set-matrix-zeroes/">week7-5.set-matrix-zeroes</a>
3+ * <li>Description: Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's</li>
4+ * <li>Topics: Array, Hash Table, Matrix </li>
5+ * <li>Time Complexity: O(MN), Runtime 0ms </li>
6+ * <li>Space Complexity: O(M+N), Memory 45.58MB </li>
7+ */
8+ class Solution {
9+ public void setZeroes (int [][] matrix ) {
10+ int m = matrix .length ;
11+ int n = matrix [0 ].length ;
12+
13+ boolean [] rowZero = new boolean [m ];
14+ boolean [] colZero = new boolean [n ];
15+
16+ for (int i = 0 ; i < m ; i ++) {
17+ for (int j = 0 ; j < n ; j ++) {
18+ if (matrix [i ][j ] == 0 ) {
19+ rowZero [i ] = true ;
20+ colZero [j ] = true ;
21+ }
22+ }
23+ }
24+
25+ for (int i = 0 ; i < m ; i ++) {
26+ if (rowZero [i ]) {
27+ for (int j = 0 ; j < n ; j ++) {
28+ matrix [i ][j ] = 0 ;
29+ }
30+ }
31+ }
32+ for (int i = 0 ; i < n ; i ++) {
33+ if (colZero [i ]) {
34+ for (int j = 0 ; j < m ; j ++) {
35+ matrix [j ][i ] = 0 ;
36+ }
37+ }
38+ }
39+
40+ }
41+ }
You can’t perform that action at this time.
0 commit comments