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.

就是说从左上到右下斜对角的元素都相等,判断一下就行

class Solution(object):
    def isToeplitzMatrix(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: bool
        """
        for i in range(1, len(matrix), 1):
            for j in range(1, len(matrix[0]), 1):
                if matrix[i][j] != matrix[i - 1][j - 1]:
                    return False
        return True
    

 

posted @ 2020-07-01 09:51  whatyouthink  阅读(91)  评论(0编辑  收藏  举报