LeetCode: Longest Line of Consecutive One in Matrix

LeetCode: Longest Line of Consecutive One in Matrix

Given a 01 matrix M, find the longest line of consecutive one in the matrix. The line could be horizontal, vertical, diagonal or anti-diagonal.

Example:

1
2
3
4
5
Input:
[[0,1,1,0],
[0,1,1,0],
[0,0,0,1]]
Output: 3

Hint: The number of elements in the given matrix will not exceed 10,000.

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
public class Solution {
public int longestLine(int[][] M) {
int result = 0;
if (M == null || M.length == 0 || M[0] == null || M[0].length == 0) return result;

for (int i = 0; i < M.length; i++) {
for (int j = 0; j < M[0].length; j++) {
int x = M[i][j];
if (x == 0) continue;
if (!(j > 0 && M[i][j - 1] == 1)) { // to right
int k = j;
int count = 0;
while (k < M[0].length && M[i][k] == 1) {
count++;
k++;
}
result = Math.max(result, count);
}
if (!(i > 0 && M[i - 1][j] == 1)) { // to down
int k = i;
int count = 0;
while (k < M.length && M[k][j] == 1) {
count++;
k++;
}
result = Math.max(result, count);
}
if (!(i > 0 && j > 0 && M[i - 1][j - 1] == 1)) {
int m = i;
int n = j;
int count = 0;
while (m < M.length && n < M[0].length && M[m][n] == 1) {
count++;
m++;
n++;
}
result = Math.max(result, count);
}
if (!(i > 0 && j < M[0].length-1 && M[i - 1][j + 1] == 1)) { // to left down
int m = i;
int n = j;
int count = 0;
while (m < M.length && n >= 0 && M[m][n] == 1) {
count++;
m++;
n--;
}
result = Math.max(result, count);
}
}
}
return result;
}
}