LeetCode 766. Toeplitz Matrix

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
bool isToeplitzMatrix(vector<vector<int>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
if (m == 1 || n == 1) return true;
int start_x = m - 2, start_y = 0;
while (start_y < n - 1) {
for (int next_x = start_x + 1, next_y = start_y + 1; next_x < m && next_y < n; ++next_x, ++next_y)
if (matrix[start_x][start_y] != matrix[next_x][next_y])
return false;
if (--start_x < 0) {
start_x = 0;
++start_y;
}
}
return true;
}
};