Skip to content

766. Toeplitz Matrix

https://leetcode.com/problems/toeplitz-matrix/

js
/**
 * @param {number[][]} matrix
 * @return {boolean}
 */
var isToeplitzMatrix = function(matrix) {
  for (let i = 0; i < matrix.length - 1; i++) {
    const r1 = matrix[i]
    const r2 = matrix[i + 1]

    for (let j = 0; j < r1.length - 1; j++) {
      if (r1[j] !== r2[j + 1]) {
        return false
      }
    }
  }

  return true
}
py
class Solution:

    def isToeplitzMatrix(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: bool
        """
        return all(matrix[i][:-1] == matrix[i + 1][1:] for i in range(len(matrix) - 1))