-
Notifications
You must be signed in to change notification settings - Fork 1
/
Solution.java
55 lines (40 loc) · 1.28 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
@lc id : 73
@problem : Set Matrix Zeroes
@author : github.com/rohitkumar-rk
@url : https://leetcode.com/problems/set-matrix-zeroes/
@difficulty : medium
*/
class Solution {
public void setZeroes(int[][] matrix) {
boolean isCol = false;
for(int i = 0; i < matrix.length; i++){
if(matrix[i][0] == 0)
isCol = true;
for(int j = 1; j < matrix[i].length; j++){
if(matrix[i][j] == 0){
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for(int row = 1; row < matrix.length; row++){
for(int col = 1; col < matrix[row].length; col++){
if(matrix[row][0] == 0 || matrix[0][col] == 0)
matrix[row][col] = 0;
}
}
//First row
if(matrix[0][0] == 0){
for(int col = 0; col < matrix[0].length; col++){
matrix[0][col] = 0;
}
}
//First column
if(isCol){
for(int row = 0; row < matrix.length; row++){
matrix[row][0] = 0;
}
}
}
}