LeetCode Solutions

766. Toeplitz Matrix

Time: $O(mn)$

Space: $O(1)$

			

class Solution {
 public:
  bool isToeplitzMatrix(vector<vector<int>>& matrix) {
    for (int i = 0; i + 1 < matrix.size(); ++i)
      for (int j = 0; j + 1 < matrix[0].size(); ++j)
        if (matrix[i][j] != matrix[i + 1][j + 1])
          return false;
    return true;
  }
};
			

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

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

    return True