Skip to content

Commit 81203ea

Browse files
refactor 562
1 parent 0e360df commit 81203ea

File tree

2 files changed

+42
-41
lines changed

2 files changed

+42
-41
lines changed

src/main/java/com/fishercoder/solutions/_562.java

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
/**
44
* 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
87
*
98
* 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.
109
@@ -20,48 +19,50 @@
2019
*/
2120
public class _562 {
2221

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];
5450
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+
}
5758
}
59+
longestLine = Math.max(longestLine, thisLine);
5860
}
59-
longestLine = Math.max(longestLine, thisLine);
6061
}
6162
}
6263
}
64+
return longestLine;
6365
}
64-
return longestLine;
6566
}
6667

6768
}

src/test/java/com/fishercoder/_562Test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
* Created by fishercoder on 4/23/17.
1111
*/
1212
public class _562Test {
13-
private static _562 test;
13+
private static _562.Solution1 solution1;
1414
private static int expected;
1515
private static int actual;
1616
private static int[][] M;
1717

1818
@BeforeClass
1919
public static void setup() {
20-
test = new _562();
20+
solution1 = new _562.Solution1();
2121
}
2222

2323
@Test
@@ -28,7 +28,7 @@ public void test1() {
2828
{0, 0, 0, 1}
2929
};
3030
expected = 3;
31-
actual = test.longestLine(M);
31+
actual = solution1.longestLine(M);
3232
assertEquals(expected, actual);
3333
}
3434

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy