Article directory
One [topic category]
- matrix
Two [question difficulty]
- Simple
Three [topic number]
- 766. Toeplitz Matrix
Four [title description]
- You are given an mxn matrix matrix. Returns true if this matrix is a Toeplitz matrix; otherwise, returns false.
- A matrix is a Toeplitz matrix if the elements on each diagonal from top left to bottom right are the same.
Five [topic examples]
-
Example 1:
- Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]
- Output: true
- explain:
- In the above matrix, its diagonal is:
- “[9]”, “[5, 5]”, “[1, 1, 1]”, “[2, 2, 2]”, “[3, 3]”, “[4]”。
- All elements on the respective diagonals are the same, so the answer is True.
-
Example 2:
- Input: matrix = [[1,2],[2,2]]
- output: false
- explain:
- Elements on the diagonal "[1, 2]" are different.
Six [topic prompt]
- m = = m a t r i x . l e n g t h m == matrix.length m==matrix.length
- n = = m a t r i x [ i ] . l e n g t h n == matrix[i].length n==matrix[i].length
- 1 < = m , n < = 20 1 <= m, n <= 20 1<=m,n<=20
- 0 < = m a t r i x [ i ] [ j ] < = 99 0 <= matrix[i][j] <= 99 0<=matrix[i][j]<=99
Seven [topic advanced]
- What if the matrix is stored on disk, and memory is limited such that at most one row of the matrix can be loaded into memory at a time?
- What if the matrix is so large that only an incomplete row can be loaded into memory at a time?
Eight [problem-solving ideas]
- The idea of this question is relatively simple. We don’t need to compare diagonals one by one. We only need to compare whether the remaining elements except the last element in the previous row and the first element in the next row want to wait
- Because if it is a Toeplitz matrix, the next row must be the matrix formed by moving the previous row to the right, so the last element of the previous row and the first element of the next row do not need to be judged, because the previous row The last element is removed, and the first element of the next row becomes the diagonal alone
- Finally return the result
Nine [time frequency]
- Time complexity: O ( m ∗ n ) O(m * n)O(m∗n), m 、 n m、n m and n are the number of rows and columns of the incoming array respectively
- Space complexity: O ( 1 ) O(1)O(1)
Ten [code implementation]
- Java language version
class Solution {
public boolean isToeplitzMatrix(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
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;
}
}
- C language version
bool isToeplitzMatrix(int** matrix, int matrixSize, int* matrixColSize)
{
int m = matrixSize;
int n = matrixColSize[0];
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;
}
- Python language version
class Solution:
def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:
m = len(matrix)
n = len(matrix[0])
for i in range(m - 1):
for j in range(n - 1):
if matrix[i][j] != matrix[i+1][j+1]:
return False
return True
- C++ language version
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;
}
};
Eleven [submission results]
-
Java language version
-
C language version
-
Python language version
-
C++ language version