-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLeftmostOneInBinaryMatrix.java
More file actions
47 lines (39 loc) · 1.05 KB
/
Copy pathLeftmostOneInBinaryMatrix.java
File metadata and controls
47 lines (39 loc) · 1.05 KB
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
package Matrix;
public class LeftmostOneInBinaryMatrix {
private static int getLeftmostOne(int[][] matrix) {
if (matrix == null) {
return -1;
}
int rows = matrix.length;
int cols = matrix[0].length;
int index = -1;
for (int i = 0; i < rows; ++i) {
int j = (index != -1) ? index : cols - 1;
for (; j >= 0; --j) {
if (matrix[i][j] == 1) {
index = j;
}
}
}
return index;
}
private static void testFunction(int[][] matrix, int expected, int i) {
if (getLeftmostOne(matrix) == expected) {
System.out.println("Test case " + i + " passed");
} else {
System.out.println("Test case" + i + " failed");
}
}
public static void main(String[] args) {
int[][] matrix1 = {{0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 1, 1},
{0, 0, 0, 0, 1, 1},
{0, 0, 0, 0, 1, 1},
{0, 0, 0, 0, 1, 1}};
int expected1 = 4;
int[][] matrix2 = null;
int expected2 = -1;
testFunction(matrix1, expected1, 1);
testFunction(matrix2, expected2, 2);
}
}