Leetcode 766 - Toeplitz Matrix

Note:

  • A straightforward problem. Just compare diagonal grids.

Question:

Given an m x n matrix, return true if the matrix is Toeplitz. Otherwise, return false.

A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements.

Example:

img

1
2
3
4
5
6
Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]
Output: true
Explanation:
In the above grid, the diagonals are:
"[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]".
In each diagonal all elements are the same, so the answer is True.

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* @param {number[][]} matrix
* @return {boolean}
*/
var isToeplitzMatrix = function(matrix) {
const rows = matrix.length;
const cols = matrix[0].length;
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (i !== 0 && j !== 0) continue;
let nx = i;
let ny = j;
while (nx < rows && ny < cols) {
if (matrix[i][j] !== matrix[nx][ny]) return false;
nx++;
ny++;
}
}
}
return true;
};