Problem: 766. Toeplitz Matrix 托普利茨矩阵
解题过程
从行列分别出发,然后+1,查看是否相等即可
Code
class Solution { public: bool isToeplitzMatrix(vector<vector<int>>& matrix) { int m = matrix.size(), n = matrix[0].size(); int x, y, r, c; for(int i = 0; i < m; i++) { x = r = i; y = c = 0; while(true) { x++; y++; if(x < 0 || y < 0 || x >= m || y >= n) { break; } if(matrix[x][y]!=matrix[r][c]) { return false; } } } for(int i = 1; i < n; i++) { x = r = 0; y = c = i; while(true) { x++; y++; if(x < 0 || y < 0 || x >= m || y >= n) { break; } if(matrix[x][y]!=matrix[r][c]) { return false; } } } return true; } };