matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element.
Now given an M x N matrix, return True if and only if the matrix is Toeplitz.若是一個矩陣的每一條斜對角線(左上到右下)上的元素都相等,則咱們稱它爲託普利茲矩陣。如今輸入一個M*N大小的矩陣,若是它是一個託普利茲矩陣,則返回true,若是不是,返回false。code
Example 1:
Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]
Output: True
Explanation:
1234
5123
9512
在上面的矩陣中, 矩陣的全部斜對角線爲 "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]", 每個對角線上的元素都相等,所以返回true。
Example 2:
Input: matrix = [[1,2],[2,2]]
Output: False
Explanation:
斜對角線 "[1, 2]" 元素的值不等,返回false。element
public boolean isToeplitzMatrix(int[][] matrix) { for(int i=0;i<matrix.length-1;i++){ for(int j=0;j<matrix[0].length-1;j++){ if(matrix[i][j] != matrix[i+1][j+1]){ return false; } } } return true; }