原题
https://leetcode-cn.com/problems/toeplitz-matrix/

思路
逐层遍历,注意:最后一行和最后一列不用遍历

题解
package com.leetcode.code;
/**
* @Description:
* @ClassName: Code766
* @Author: ZK
* @Date: 2021/2/22 00:45
* @Version: 1.0
*/
public class Code766 {
public static void main(String[] args) {
int[][] matrix = {{1,2,3,4},{5,1,2,3},{9,5,1,2}};
System.out.println(isToeplitzMatrix(matrix));
}
public static boolean isToeplitzMatrix(int[][] matrix) {
int row = matrix.length;
int col = matrix[0].length;
for (int i = 0; i < row-1; i++) {
for (int j = 0; j < col-1; j++) {
if (matrix[i][j] != matrix[i+1][j+1]) {
return false;
}
}
}
return true;
}
}
这篇博客介绍了如何解决LeetCode上的766题——判断一个矩阵是否为托普利茨矩阵。作者提供了一个简单的Java解决方案,通过逐层遍历矩阵,检查每对相邻元素是否相等来确定。该算法避免了遍历最后一行和最后一列,提高了效率。
541

被折叠的 条评论
为什么被折叠?



