-
Notifications
You must be signed in to change notification settings - Fork 5
/
766. Toeplitz Matrix.java
48 lines (48 loc) · 1.45 KB
/
766. Toeplitz Matrix.java
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
class Solution {
public boolean isToeplitzMatrix(int[][] matrix) {
int R = matrix.length;
int C = matrix[0].length;
// check for upper right corners
for(int col=0; col < C; col++) {
boolean f = checkUpperDiagonal(matrix, col);
if(!f) return false;
}
// check for lower left corners
for(int row=0; row < R; row++) {
boolean f = checkLowerDiagonal(matrix, row);
if(!f) return false;
}
return true;
}
private boolean checkLowerDiagonal(int[][] matrix, int row) {
// TODO Auto-generated method stub
int R = matrix.length;
int C = matrix[0].length;
int felem=matrix[row][0];
for(int col=0; row < R && col < C; row++, col++) {
int selem=matrix[row][col];
if(felem != selem) return false;
}
return true;
}
private boolean checkUpperDiagonal(int[][]matrix, int col) {
// TODO Auto-generated method stub
int R = matrix.length;
int C = matrix[0].length;
int felem=matrix[0][col];
for(int row = 0; row < R && col < C; row++, col++) {
int selem=matrix[row][col];
if(felem!=selem) return false;
}
return true;
}
}