# 766. 托普利茨矩阵

<https://leetcode-cn.com/problems/toeplitz-matrix/>

## &#x20;解法一：

遍历整个矩阵, 看每个元素\[i,j]的右下方元素\[i+1,j+1]是否与其相等即可

```cpp
class Solution {
public:
    bool isToeplitzMatrix(vector<vector<int>>& matrix) {
        bool flag = true; //标志位
        int m = matrix.size(); //行数
        int n = matrix[0].size(); //列数
        for (int i = 0; i < m-1; i++) {
            for (int j = 0; j < n-1; j++) {
                if (matrix[i][j] != matrix[i+1][j+1]) //若发现不满足,标志位置0
                    flag = false;
            }
        }
        return flag;
    }
};
```

## 解法二：（进阶）

题目要求每次只读入一行，观察可知，自上而下，每行**向右平移**了一位。掐头去尾比较上下两行

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cai-sen-se.gitbook.io/leetcode/601-800/766.-tuo-pu-li-ju-zhen.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
