leetcode 766. 托普利茨矩阵

题解

遍历数组matrix,比较当前元素与下一个对角线元素

示例代码(go)

1
2
3
4
5
6
7
8
9
10
11
12
13
func isToeplitzMatrix(matrix [][]int) bool {
isToeplitz := true
m, n := len(matrix[0]), len(matrix)
for i := 0; i < n; i++ {
for j := 0; j < m; j++ {
if (i+1) < n && (j+1) < m && matrix[i][j] != matrix[i+1][j+1] {
isToeplitz = false
break
}
}
}
return isToeplitz
}