---
# System prepended metadata

title: 0766. Toeplitz Matrix
tags: [Leetcode, FaceBook, Easy]

---

# 0766. Toeplitz Matrix
###### tags: `Leetcode` `FaceBook` `Easy`
Link: https://leetcode.com/problems/toeplitz-matrix/
## 思路
利用同一条对角线上的元素 row num - col num都相同
## Code
### 思路一 用HashMap
```java=
class Solution {
    public boolean isToeplitzMatrix(int[][] matrix) {
        Map<Integer,Integer> map = new HashMap<>();
        for(int i = 0;i < matrix.length;i++){
            for(int j = 0;j < matrix[0].length;j++){
                if(!map.containsKey(i-j)){
                    map.put(i-j, matrix[i][j]);
                }
                else if(map.get(i-j)!=matrix[i][j]){
                    return false;
                }
            }
        }
        return true;
    }
}
```
### 思路二 不用HashMap 直接遍历比较
```java=
class Solution {
    public boolean isToeplitzMatrix(int[][] matrix) {
        Map<Integer,Integer> map = new HashMap<>();
        for(int i = 0;i < matrix.length;i++){
            for(int j = 0;j < matrix[0].length;j++){
                if(i>0 && j>0 && matrix[i-1][j-1]!=matrix[i][j]){
                    return false;
                }
            }
        }
        return true;
    }
}
```