Toeplitz Matrix

ㅋㅋ·2022년 10월 31일
0

알고리즘-leetcode

목록 보기
42/135

0 ~ 99 사이의 수가 담긴 2차원 벡터 행렬을 받고,

해당 행렬이 Toeplitz를 만족하는지 true, false를 반환하는 문제이다.

Toeplitz는 모든 대각선들이 각각 대각선마다 같은 수를 가지고 있는 행렬이라고 한다.

A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements.

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

        return true;
    }
};

0개의 댓글