-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathZeroMatrix.java
51 lines (46 loc) · 1.37 KB
/
ZeroMatrix.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
package arraysandstrings;
/**
* ZeroMatrix
* https://github.com/careercup/CtCI-6th-Edition/tree/master/Java/Ch%2001.%20Arrays%20and%20Strings/Q1_08_Zero_Matrix
* Difficulty : Easy
* Related Topics : Array, String
*
* created by Cenk Canarslan on 2021-10-25
*/
public class ZeroMatrix {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{5, 6, 7},
{9, 10,11},
{13,14,15}};
// Y
// ^
// | 1, 2, 3
// | 5, 6, 7
// | 9, 10, 11
// | 13, 14, 15
// |-------------> X
setMatrix(matrix, 3, 2);
}
public static void setMatrix(int[][] matrix, int col, int row) {
System.out.println("element = " + matrix[col][row]);
int height = matrix.length;
int width = matrix[0].length;
System.out.println("height, width = " + height + ", " + width);
for (int i = 0; i < width; i++) {
matrix[col][i] = 0;
}
for (int j = 0; j < height; j++) {
matrix[j][row] = 0;
}
printMatrix(matrix);
}
private static void printMatrix(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.println("matrix["+i+"]["+j+"] = " + matrix[i][j]);
}
}
}
}