forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_562.java
51 lines (48 loc) · 1.89 KB
/
_562.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 com.fishercoder.solutions;
public class _562 {
public static class Solution1 {
public int longestLine(int[][] M) {
if (M == null || M.length == 0) {
return 0;
}
int[][] directions = new int[][]{
{-1, 0},
{-1, 1},
{0, 1},
{1, 1},
{1, 0},
{1, -1},
{0, -1},
{-1, -1},
};
int longestLine = 0;
int m = M.length;
int n = M[0].length;
int[][][] cache = new int[m][n][8];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (M[i][j] == 1) {
for (int k = 0; k < directions.length; k++) {
int nextI = i + directions[k][0];
int nextJ = j + directions[k][1];
int thisLine = 1;
if (nextI >= 0 && nextI < m && nextJ >= 0 && nextJ < n && cache[nextI][nextJ][k] != 0) {
thisLine += cache[nextI][nextJ][k];
cache[i][j][k] = thisLine;
} else {
while (nextI >= 0 && nextI < m && nextJ >= 0 && nextJ < n && M[nextI][nextJ] == 1) {
thisLine++;
cache[i][j][k] = thisLine;
nextI += directions[k][0];
nextJ += directions[k][1];
}
}
longestLine = Math.max(longestLine, thisLine);
}
}
}
}
return longestLine;
}
}
}