왼쪽 위에서 오른쪽 아래까지의 모든 대각선이 동일한 요소를 갖는 경우 행렬은 Toeplitz입니다.
예시 1
[[1,2,3,4], [5,1,2,3], [9,5,1,2]]
출력 -
true
위의 그리드에서 대각선은 -
"[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]".
각 대각선에서 모든 요소는 동일하므로 정답은 참입니다.
예시 2
Input: matrix [[1,2], [2,2]]
출력 -
false
대각선 "[1, 2]"에는 다른 요소가 있습니다.
코드
public class Matrix
{
public bool ToeplitzMatrix(int[,] mat)
{
int row = getMatrixRowSize(mat);
int col = getMatrixColSize(mat);
for (int i = 1; i < row; i++)
{
for (int j = 1; j < col; j++)
{
if (mat[i, j] != mat[i - 1, j - 1])
{
return false;
}
}
}
return true;
}
private int getMatrixRowSize(int[,] mat)
{
return mat.GetLength(0);
}
private int getMatrixColSize(int[,] mat)
{
return mat.GetLength(1);
}
}
static void Main(string[] args)
{
Matrix m = new Matrix();
int[,] mat = new int[3, 4] { { 1, 2, 3, 4 }, { 5, 1, 2, 3 }, { 9, 5, 1, 2 } };
Console.WriteLine(m.ToeplitzMatrix(mat));
} 출력
TRUE