-
Notifications
You must be signed in to change notification settings - Fork 0
/
766. Toeplitz Matrix
34 lines (33 loc) · 1.11 KB
/
766. Toeplitz Matrix
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
public class Solution {
public boolean isToeplitzMatrix(int[][] matrix) {
// верхний треугольник
for (int i = matrix[0].length - 1; i >= 0; i--) {
int y = 0;
int x = i;
int current = matrix[y][x];
int nextDiagonal = current;
while (y + 1 < matrix.length && x + 1 < matrix[y + 1].length) {
current = nextDiagonal;
nextDiagonal = matrix[++y][++x];
if (current != nextDiagonal) {
return false;
}
}
}
//нижний треугольник
for (int i = 1; i < matrix.length; i++) {
int y = i;
int x = 0;
int current = matrix[y][x];
int nextDiagonal = current;
while (y + 1 < matrix.length && x + 1 < matrix[y + 1].length) {
current = nextDiagonal;
nextDiagonal = matrix[++y][++x];
if (current != nextDiagonal) {
return false;
}
}
}
return true;
}
}