# C#LeetCode刷题之#766-托普利茨矩阵（Toeplitz Matrix）

matrix = [
[1,2,3,4],
[5,1,2,3],
[9,5,1,2]
]

matrix = [
[1,2],
[2,2]
]

matrix 是一个包含整数的二维数组。
matrix 的行数和列数均在 [1, 20]范围内。
matrix[i][j] 包含的整数在 [0, 99]范围内。

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

Now given an m * n matrix, return True if and only if the matrix is Toeplitz.

Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]

Output: True

Explanation:
1234
5123
9512

In the above grid, the diagonals are “[9]”, “[5, 5]”, “[1, 1, 1]”, “[2, 2, 2]”, “[3, 3]”, “[4]”, and in each diagonal all elements are the same, so the answer is True.

Input: matrix = [[1,2],[2,2]]

Output: False

Explanation:The diagonal “[1, 2]” has different elements.

Note:

matrix will be a 2D array of integers.
matrix will have a number of rows and columns in range [1, 20].
matrix[i][j] will be integers in range [0, 99].

```public class Program {

public static void Main(string[] args) {
int[,] cost = null;

cost = new int[,] {{ 1, 2, 3, 4 },
{ 5, 1, 2, 3},
{ 9, 5, 1, 2}
};
var res = IsToeplitzMatrix(cost);
Console.WriteLine(res);

}

private static bool IsToeplitzMatrix(int[,] matrix) {
//暴力解法
for(int i = 0; i < matrix.GetLength(0) - 1; i++) {
for(int j = 0; j < matrix.GetLength(1) - 1; j++) {
if(matrix[i + 1, j + 1] != matrix[i, j]) {
return false;
}
}
}
return true;
}

}```

`True`