-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path562. LongestLineofConsecutiveOneinMatrix.h
More file actions
49 lines (36 loc) · 1.28 KB
/
562. LongestLineofConsecutiveOneinMatrix.h
File metadata and controls
49 lines (36 loc) · 1.28 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
/*
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:
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.
*/
int longestLine(vector<vector<int>>& M) {
if (M.empty() || M[0].empty()) {
return 0;
}
int m = M.size(), n = M[0].size();
int ret = 0;
vector<vector<int>> dirs {{1, 0}, {0, 1}, {-1, -1}, {-1, 1}};
for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++) {
if (M[i][j] == 0) {
continue;
}
for (int k = 0; k < 4; k++) {
int count = 0;
int x = i, y = j;
while (x >= 0 && x < m && y >= 0 && y < n && M[x][y] == 1) {
x += dirs[k][0];
y += dirs[k][1];
count++;
}
ret = max(ret, count);
}
}
}
return ret;
}