-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_562.java
More file actions
100 lines (96 loc) · 2.24 KB
/
Copy pathleetcode_562.java
File metadata and controls
100 lines (96 loc) · 2.24 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package leetcode;
/**
* Created by bosun on 4/22/17.
*/
public class leetcode_562 {
int max = 0;
public int longestLine(int[][] M) {
int n = M.length, m = M[0].length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if ((M[i][j] & 1) == 1) {
getLines(M, i, j);
}
}
}
return max;
}
private void getLines(int[][] M, int i, int j) {
int n = M.length, m = M[0].length;
if (i < 0 || i >= n || j < 0 || j >= m || M[i][j] == 0 || M[i][j] >> 1 == 15) {
return;
}
int mark = M[i][j] >> 1;
if ((mark & 1) == 0) {
int temp = 0;
for (int k = j; k < m; k++) {
if (M[i][k] == 0) break;
M[i][k] |= (1 << 1);
temp++;
}
for (int k = j - 1; k >= 0; k--) {
if (M[i][k] == 0) break;
M[i][k] |= (1 << 1);
temp++;
}
max = Math.max(temp, max);
}
if ((mark & (1 << 1)) == 0) {
int temp = 0;
for (int k = i; k < n; k++) {
if (M[k][j] == 0) break;
M[k][j] |= (1 << 2);
temp++;
}
for (int k = i - 1; k >= 0; k--) {
if (M[k][j] == 0) break;
M[k][j] |= (1 << 2);
temp++;
}
max = Math.max(temp, max);
}
if ((mark & (1 << 3)) == 0) {
int temp = 0;
for (int k = i, l = j; k >= 0 && l < m; ) {
if (M[k][l] == 0) break;
M[k][l] |= (1 << 3);
temp++;
k--;
l++;
}
for (int k = i + 1, l = j - 1; k < n && l >= 0; ) {
if (M[k][l] == 0) break;
M[k][l] |= (1 << 3);
temp++;
k++;
l--;
}
max = Math.max(temp, max);
}
if ((mark & (1 << 4)) == 0) {
int temp = 0;
for (int k = i, l = j; k >= 0 && l >= 0; ) {
if (M[k][l] == 0) break;
M[k][l] |= (1 << 4);
temp++;
k--;
l--;
}
for (int k = i + 1, l = j + 1; k < n && l < m; ) {
if (M[k][l] == 0) break;
M[k][l] |= (1 << 4);
temp++;
k++;
l++;
}
max = Math.max(temp, max);
}
}
public static void main(String[] args) {
new leetcode_562().longestLine(new int[][]{
{0, 1, 1, 0},
{0, 1, 1, 0},
{0, 0, 0, 1}
});
}
}