LeetCode 766: Toeplitz Matrix (Diagonal Neighbor Consistency Check)
LeetCode 766MatrixSimulationToday we solve LeetCode 766 - Toeplitz Matrix.
Source: https://leetcode.com/problems/toeplitz-matrix/
English
Problem Summary
A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same value. Return true if the given matrix is Toeplitz, otherwise return false.
Key Insight
Each cell (except first row and first column) only needs to match its top-left neighbor. So for all r > 0 and c > 0, verify matrix[r][c] == matrix[r-1][c-1].
Algorithm
- Start from row 1 to end.
- Start from column 1 to end.
- If current value differs from top-left value, return false immediately.
- If all checks pass, return true.
Complexity Analysis
Let matrix size be m × n.
Time: O(mn).
Space: O(1).
Common Pitfalls
- Comparing wrong direction (e.g., top-right instead of top-left).
- Starting loops from 0, which causes boundary issues.
- Overcomplicating with hash maps when constant space is enough.
Reference Implementations (Java / Go / C++ / Python / JavaScript)
class Solution {
public boolean isToeplitzMatrix(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
for (int r = 1; r < m; r++) {
for (int c = 1; c < n; c++) {
if (matrix[r][c] != matrix[r - 1][c - 1]) {
return false;
}
}
}
return true;
}
}func isToeplitzMatrix(matrix [][]int) bool {
m, n := len(matrix), len(matrix[0])
for r := 1; r < m; r++ {
for c := 1; c < n; c++ {
if matrix[r][c] != matrix[r-1][c-1] {
return false
}
}
}
return true
}class Solution {
public:
bool isToeplitzMatrix(vector<vector<int>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
for (int r = 1; r < m; ++r) {
for (int c = 1; c < n; ++c) {
if (matrix[r][c] != matrix[r - 1][c - 1]) {
return false;
}
}
}
return true;
}
};class Solution:
def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:
m, n = len(matrix), len(matrix[0])
for r in range(1, m):
for c in range(1, n):
if matrix[r][c] != matrix[r - 1][c - 1]:
return False
return Truevar isToeplitzMatrix = function(matrix) {
const m = matrix.length;
const n = matrix[0].length;
for (let r = 1; r < m; r++) {
for (let c = 1; c < n; c++) {
if (matrix[r][c] !== matrix[r - 1][c - 1]) {
return false;
}
}
}
return true;
};中文
题目概述
如果一个矩阵从左上到右下的每条对角线上的元素都相同,那么它就是 Toeplitz 矩阵。给定矩阵,判断是否满足该性质。
核心思路
除了第一行和第一列,任意位置 (r, c) 都必须和其左上角 (r-1, c-1) 相等。只要出现一次不相等就可以直接返回 false。
算法步骤
- 从第 1 行开始遍历到末尾。
- 从第 1 列开始遍历到末尾。
- 检查 matrix[r][c] 是否等于 matrix[r-1][c-1]。
- 若全部检查通过,则返回 true。
复杂度分析
设矩阵大小为 m × n。
时间复杂度:O(mn)。
空间复杂度:O(1)。
常见陷阱
- 对角比较方向写错(应比较左上角)。
- 循环从 0 开始导致越界判断复杂化。
- 不必要地引入额外哈希结构,增加空间开销。
多语言参考实现(Java / Go / C++ / Python / JavaScript)
class Solution {
public boolean isToeplitzMatrix(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
for (int r = 1; r < m; r++) {
for (int c = 1; c < n; c++) {
if (matrix[r][c] != matrix[r - 1][c - 1]) {
return false;
}
}
}
return true;
}
}func isToeplitzMatrix(matrix [][]int) bool {
m, n := len(matrix), len(matrix[0])
for r := 1; r < m; r++ {
for c := 1; c < n; c++ {
if matrix[r][c] != matrix[r-1][c-1] {
return false
}
}
}
return true
}class Solution {
public:
bool isToeplitzMatrix(vector<vector<int>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
for (int r = 1; r < m; ++r) {
for (int c = 1; c < n; ++c) {
if (matrix[r][c] != matrix[r - 1][c - 1]) {
return false;
}
}
}
return true;
}
};class Solution:
def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:
m, n = len(matrix), len(matrix[0])
for r in range(1, m):
for c in range(1, n):
if matrix[r][c] != matrix[r - 1][c - 1]:
return False
return Truevar isToeplitzMatrix = function(matrix) {
const m = matrix.length;
const n = matrix[0].length;
for (let r = 1; r < m; r++) {
for (let c = 1; c < n; c++) {
if (matrix[r][c] !== matrix[r - 1][c - 1]) {
return false;
}
}
}
return true;
};
Comments