link
https://leetcode-cn.com/problems/toeplitz-matrix/
time consuming
Problem solving: 5 min
Problem solving: 6 min
Title
Give you a matrix of mxn. If this matrix is a Toplitz matrix, return true; otherwise, return false.
If the elements on each diagonal from the upper left to the lower right of the matrix are the same, then the matrix is a Toplitz matrix.
prompt:
- m == matrix.length
- n == matrix[i].length
- 1 <= m, n <= 20
- 0 <= matrix[i][j] <= 99
Ideas
According to the definition, check whether all elements are equal to the lower right element. If all elements in the matrix are satisfied, it is the Toplitz matrix.
Time complexity: O (m ∗ n) O(m*n)O ( m∗n)
AC code
class Solution {
public:
bool isToeplitzMatrix(vector<vector<int>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
for(int i = 0; i < m-1; ++i) {
for(int j = 0; j < n-1; ++j) {
if(matrix[i][j] != matrix[i+1][j+1]) {
return false;
}
}
}
return true;
}
};