-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.java
35 lines (32 loc) · 998 Bytes
/
Main.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
package com.company;
public class Main {
public static void main(String[] args) {
// write your code here
}
public void setZeroes(int[][] matrix) {
var isZero = false;
for (int r = 0; r < matrix.length; r++) {
isZero = isZero || matrix[r][0] == 0;
for (int c = 1; c < matrix[0].length; c++) {
if (matrix[r][c] == 0) {
matrix[r][0] = 0;
matrix[0][c] = 0;
}
}
}
for (int r = 1; r < matrix.length; r++) {
for (int c = 1; c < matrix[0].length; c++) {
if (matrix[r][0] == 0 || matrix[0][c] == 0)
matrix[r][c] = 0;
}
}
if (matrix[0][0] == 0) {
for (int c = 0; c < matrix[0].length; c++)
matrix[0][c] = 0;
}
if (isZero) {
for (int r = 0; r < matrix.length; r++)
matrix[r][0] = 0;
}
}
}