package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * @ClassName: IsToeplitzMatrix * @Author: xiaof * @Description: 766. Toeplitz Matrix * A 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. * * Input: * matrix = [ * [1,2,3,4], * [5,1,2,3], * [9,5,1,2] * ] * Output: True * Explanation: * In the above grid, the diagonals are: * "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]". * In each diagonal all elements are the same, so the answer is True. * * @Date: 2019/7/4 19:47 * @Version: 1.0 */ public class IsToeplitzMatrix { public boolean solution(int[][] matrix) { //就是比较斜线上是否是通一个数据 //每一斜线 //每次可以和下一行的斜线比较,这样依次比较 for(int i = 0; i < matrix.length - 1; ++i) { for(int j = 0; j < matrix[i].length - 1; ++j) { if(matrix[i][j] != matrix[i + 1][j + 1]) { return false; } } } return true; } public boolean isToeplitzMatrix(int[][] matrix) { for (int i = 0; i < matrix.length - 1; i++) { for (int j = 0; j < matrix[i].length - 1; j++) { if (matrix[i][j] != matrix[i + 1][j + 1]) return false; } } return true; } public static void main(String args[]) { int[][] matrix = {{1,2,3,4},{5,1,2,3},{9,5,1,2}}; IsToeplitzMatrix fuc = new IsToeplitzMatrix(); System.out.println(fuc.isToeplitzMatrix(matrix)); } }