-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
31 lines (31 loc) · 722 Bytes
/
Copy pathmain.js
File metadata and controls
31 lines (31 loc) · 722 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* @param {number[][]} matrix
* @return {boolean}
*/
var isToeplitzMatrix = function (matrix) {
function inside (x, y) {
return x >= 0 && x < matrix.length && y >= 0 && y < matrix[0].length
}
if (matrix == null || matrix.length === 0 || matrix[0].length === 0) return true
for (let i = 0; i < matrix[0].length; i++) {
const elt = matrix[0][i]
let x = 1
let y = i + 1
while (inside(x, y)) {
if (elt !== matrix[x][y]) return false
x++
y++
}
}
for (let i = 1; i < matrix.length; i++) {
const elt = matrix[i][0]
let x = i + 1
let y = 1
while (inside(x, y)) {
if (elt !== matrix[x][y]) return false
x++
y++
}
}
return true
}