|
2 | 2 |
|
3 | 3 | /**
|
4 | 4 | * Created by fishercoder on 4/23/17.
|
5 |
| - */ |
6 |
| - |
7 |
| -/**Longest Line of Consecutive One in Matrix |
| 5 | + * |
| 6 | + * 562. Longest Line of Consecutive One in Matrix |
8 | 7 | *
|
9 | 8 | * 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.
|
10 | 9 |
|
|
20 | 19 | */
|
21 | 20 | public class _562 {
|
22 | 21 |
|
23 |
| - public int longestLine(int[][] M) { |
24 |
| - if (M == null || M.length == 0) { |
25 |
| - return 0; |
26 |
| - } |
27 |
| - int[][] directions = new int[][]{ |
28 |
| - {-1, 0}, |
29 |
| - {-1, 1}, |
30 |
| - {0, 1}, |
31 |
| - {1, 1}, |
32 |
| - {1, 0}, |
33 |
| - {1, -1}, |
34 |
| - {0, -1}, |
35 |
| - {-1, -1}, |
36 |
| - }; |
37 |
| - int longestLine = 0; |
38 |
| - int m = M.length; |
39 |
| - int n = M[0].length; |
40 |
| - int[][][] cache = new int[m][n][8]; |
41 |
| - for (int i = 0; i < m; i++) { |
42 |
| - for (int j = 0; j < n; j++) { |
43 |
| - if (M[i][j] == 1) { |
44 |
| - for (int k = 0; k < directions.length; k++) { |
45 |
| - int nextI = i + directions[k][0]; |
46 |
| - int nextJ = j + directions[k][1]; |
47 |
| - int thisLine = 1; |
48 |
| - if (nextI >= 0 && nextI < m && nextJ >= 0 && nextJ < n && cache[nextI][nextJ][k] != 0) { |
49 |
| - thisLine += cache[nextI][nextJ][k]; |
50 |
| - cache[i][j][k] = thisLine; |
51 |
| - } else { |
52 |
| - while (nextI >= 0 && nextI < m && nextJ >= 0 && nextJ < n && M[nextI][nextJ] == 1) { |
53 |
| - thisLine++; |
| 22 | + public static class Solution1 { |
| 23 | + public int longestLine(int[][] M) { |
| 24 | + if (M == null || M.length == 0) { |
| 25 | + return 0; |
| 26 | + } |
| 27 | + int[][] directions = new int[][]{ |
| 28 | + {-1, 0}, |
| 29 | + {-1, 1}, |
| 30 | + {0, 1}, |
| 31 | + {1, 1}, |
| 32 | + {1, 0}, |
| 33 | + {1, -1}, |
| 34 | + {0, -1}, |
| 35 | + {-1, -1}, |
| 36 | + }; |
| 37 | + int longestLine = 0; |
| 38 | + int m = M.length; |
| 39 | + int n = M[0].length; |
| 40 | + int[][][] cache = new int[m][n][8]; |
| 41 | + for (int i = 0; i < m; i++) { |
| 42 | + for (int j = 0; j < n; j++) { |
| 43 | + if (M[i][j] == 1) { |
| 44 | + for (int k = 0; k < directions.length; k++) { |
| 45 | + int nextI = i + directions[k][0]; |
| 46 | + int nextJ = j + directions[k][1]; |
| 47 | + int thisLine = 1; |
| 48 | + if (nextI >= 0 && nextI < m && nextJ >= 0 && nextJ < n && cache[nextI][nextJ][k] != 0) { |
| 49 | + thisLine += cache[nextI][nextJ][k]; |
54 | 50 | cache[i][j][k] = thisLine;
|
55 |
| - nextI += directions[k][0]; |
56 |
| - nextJ += directions[k][1]; |
| 51 | + } else { |
| 52 | + while (nextI >= 0 && nextI < m && nextJ >= 0 && nextJ < n && M[nextI][nextJ] == 1) { |
| 53 | + thisLine++; |
| 54 | + cache[i][j][k] = thisLine; |
| 55 | + nextI += directions[k][0]; |
| 56 | + nextJ += directions[k][1]; |
| 57 | + } |
57 | 58 | }
|
| 59 | + longestLine = Math.max(longestLine, thisLine); |
58 | 60 | }
|
59 |
| - longestLine = Math.max(longestLine, thisLine); |
60 | 61 | }
|
61 | 62 | }
|
62 | 63 | }
|
| 64 | + return longestLine; |
63 | 65 | }
|
64 |
| - return longestLine; |
65 | 66 | }
|
66 | 67 |
|
67 | 68 | }
|
0 commit comments