766

发布时间 2023-08-26 18:14:43作者: zhouzhou0615
class Solution {
    public boolean isToeplitzMatrix(int[][] matrix) {
        if (null == matrix || matrix.length == 0 || matrix[0].length == 0) {
            return true;
        }
        int row = matrix.length;
        int col = matrix[0].length;


        for(int i=0; i<row; i++) {
            int pivot = matrix[i][0];
            int[] index = {i,0};
            for(int j=1; j<row; j++) {
                int nextR = index[0] + j;
                int nextC = index[1] + j;
                if (nextR >= row || nextC >= col) {
                    break;
                } 
                if (matrix[nextR][nextC] == pivot) continue;
                else return false;
            }
        }

        for(int i=0; i<col; i++) {
            int pivot = matrix[0][i];
            int[] index = {0,i};
            for(int j=1; j<col; j++) {
                int nextR = index[0] + j;
                int nextC = index[1] + j;
                if (nextR >= row || nextC >= col) {
                    break;
                } 
                if (matrix[nextR][nextC] == pivot) continue;
                else return false;
            }
        }


        return true;
    }
}

  

 

class Solution {
    public boolean isToeplitzMatrix(int[][] matrix) {
        int m = matrix.length, n = matrix[0].length;
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                if (matrix[i][j] != matrix[i - 1][j - 1]) {
                    return false;
                }
            }
        }
        return true;
    }
}

作者:力扣官方题解
链接:https://leetcode.cn/problems/toeplitz-matrix/solutions/613732/tuo-pu-li-ci-ju-zhen-by-leetcode-solutio-57bb/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。