A Matrix is Toeplitz if every diagonal from top-left to bottom-right have the same element.
Now given a M x N matrix, return True if and only if the matrix is Toeplitz.
Example 1:
Input:
Matrix = [
[1,2,3,4],
[5,1,2,3],
[9,5,1,2]]
Output:true
Explanation:
In the above grid, the diagonals is:
"[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]".
In each diagonal all elements was the same, so the answer is True.
Example 2:
Input:
Matrix = [
[+],
[2,2]]
Output:false
Explanation:
The diagonal "[1, 2]" has different elements.
Note:
- Matrix would be a 2D array of integers.
- Matrix would has a number of rows and columns in range [1, 20].
- MATRIX[I][J] would be integers in range [0, 99].
Follow up:
- What if the matrix was stored on disk, and the memory is limited such so can only load at most one row of the matrix Into the memory at once?
- What if the matrix was so large, can only load up a partial row to the the memory at once?
class Solution: def isToeplitzMatrix(self, matrix): """ :type matrix: List[List[int]] :rtype: bool """ if len(matrix)==0: return True for i in range(1,len(matrix)): for j in range(1,len(matrix[0])): if matrix[i][j]!=matrix[i-1][j-1]: return False return True
766. Toeplitz Matrix