Skip to content

Commit e34fe26

Browse files
committed
set-matrix-zeroes solution
1 parent 4c7f657 commit e34fe26

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public void setZeroes(int[][] matrix) {
3+
/**
4+
1.๋ฌธ์ œ: 0์ด ์กด์žฌํ•˜๋Š” ์œ„์น˜์˜ ๋ชจ๋“  row, column์„ 0์œผ๋กœ set
5+
2.constraints:
6+
- m,n min = 1, max = 200
7+
- space: O(mn)์œผ๋กœ ํ’€์ดํ•˜์ง€๋ง ๊ฒƒ,
8+
3.solution
9+
- 0์˜ ์œ„์น˜๋ฅผ ํ™•์ธ -> 0์˜ ์œ„์น˜๋Š” ์—ฌ๋Ÿฌ๊ฐœ์ผ ์ˆ˜ ์žˆ์Œ
10+
- row, col ๊ฐ๊ฐ 0์˜ ์œ„์น˜ ์ €์žฅ
11+
- time: O(mn), space O(m+n)
12+
*/
13+
int m = matrix.length;
14+
int n = matrix[0].length;
15+
int[] row = new int[m]; //0์ด ์กด์žฌํ•˜๋Š” row ์œ„์น˜์ด๋ฉด 1, ์•„๋‹ˆ๋ฉด 0
16+
int[] col = new int[n]; //0์ด ์กด์žฌํ•˜๋Š” col ์œ„์น˜์ด๋ฉด 1, ์•„๋‹ˆ๋ฉด 0
17+
18+
int x = 0; int y = 0; //0์˜ ์œ„์น˜
19+
20+
for(int i = 0; i < m; i++) {
21+
for(int j = 0; j < n; j++) {
22+
if(matrix[i][j] == 0) {
23+
row[i] = 1;
24+
col[j] = 1;
25+
}
26+
}
27+
}
28+
29+
for(int i = 0; i < m; i++) {
30+
for(int j = 0; j < n; j++) {
31+
if(row[i] == 1 || col[j] == 1) {
32+
matrix[i][j] = 0;
33+
}
34+
}
35+
}
36+
}
37+
}

0 commit comments

Comments
ย (0)