Don't think about a lot, this is a very easy problem, time complexity is O(m*n)

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

    }

Or the similar one:

    public boolean isToeplitzMatrix(int[][] matrix) {
        if (matrix == null || matrix[0].length == 0)
            return true;
        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;

    }

 

posted on 2022-02-02 14:18  阳光明媚的菲越  阅读(19)  评论(0编辑  收藏  举报