LeetCode 766. 托普利茨矩阵 Toeplitz Matrix (Easy)

LeetCode 766. 托普利茨矩阵 Toeplitz Matrix (Easy)

 

 

 

class Solution {
public:
    bool isToeplitzMatrix(vector<vector<int>>& matrix) {
        
        for (int i = 0; i < matrix.size(); ++i)
        {
            if (!check(matrix, matrix[i][0], i, 0))
                return false;
        }
        for (int i = 0; i < matrix[0].size(); ++i)
        {
            if (!check(matrix, matrix[0][i], 0, i))
                return false;
        }
        return true;
    }

    bool check(vector<vector<int>>& matrix, int expectVal, int row, int col)
    {
        if (row >= matrix.size() || col >= matrix[0].size())
            return true;
        if (matrix[row][col] != expectVal)
            return false;
        return check(matrix, expectVal, row + 1, col + 1);
    }
};

 

上一篇:Leetcode 766 托普利茨矩阵


下一篇:【LEETCODE】45、766. Toeplitz Matrix