A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Given a matrix, check if it is toeplitz. (Assume the matrix is not empty)
Java Solution
public boolean isToeplitzMatrix(int[][] matrix) { int m=matrix.length; int n=matrix[0].length; for(int i=0; i<m; i++){ for(int j=0; j<n; j++){ if(i+1<m && j+1<n && matrix[i][j]!=matrix[i+1][j+1]){ return false; } } } return true; } |