lintcode1042. Toeplitz matrix

Toeplitz matrix "means that if each element from the top left to the bottom right corner of the same one of the main oblique matrices are equal.
Given a M x N matrix, determines whether" Toeplitz matrix. "

样例
样例 1:

输入: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]
输出: True
解释:
1234
5123
9512

在上述矩阵中,主斜线上元素分别为 "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]", 每一条主斜线上元素都相等,所以返回`True`.


样例 2:

输入: matrix = [[1,2],[2,2]]
输出: False
解释:
主斜线 "[1, 2]" 有不同的元素.
注意事项
matrix 是一个二维整数数组.
matrix 的行列范围都为 [1, 20].
matrix[i][j] 的整数取值范围为[0, 99].
class Solution {
public:
    /**
     * @param matrix: the given matrix
     * @return: True if and only if the matrix is Toeplitz
     */
    bool isToeplitzMatrix(vector<vector<int>> &matrix) {
        // Write your code here
        for (int i = 0; i < matrix.size(); i++) {
            /* code */
            for(int j=0; j < matrix.size(); j++)
            {
                int tmp=matrix[i][j];
                int m=i;
                int n=j;
                while(m<matrix.size()&&n<matrix[m].size())
                {
                    if(matrix[m][n]!=tmp) return false;
                    m+=1;
                    n+=1;
                }
            }
        }
        return true;
    }
};
Published 335 original articles · won praise 13 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43981315/article/details/103950473