//对角线解法
class Solution {
public:
bool isToeplitzMatrix(vector<vector<int>>& matrix) {
int i,j,a=0,b=0;
for(i=0;i<matrix.size();i++)
{
for(j=0;j<matrix[0].size();j++)
{
a=i;
b=j;
while(a<matrix.size()-1&&b<matrix[0].size()-1)
{
if(matrix[a][b]!=matrix[++a][++b])
return false;
}
}
}
return true;
}
};
![](https://i-blog.csdnimg.cn/blog_migrate/fee27a44e1580942197f15058c8626e3.png)
//与左上角比较
class Solution {
public:
bool isToeplitzMatrix(vector<vector<int>>& matrix) {
if(matrix.size()==1||matrix[0].size()==1) return true;
for(int i=1;i<matrix.size();i++)
for(int j=1;j<matrix[0].size();j++)
{
if(matrix[i][j]!=matrix[i-1][j-1])
return false;
}
return true;
}
};
![](https://i-blog.csdnimg.cn/blog_migrate/b8b18cadbe0d8e5fc80ac7f4803211a6.png)