如何使用 C# 检查给定的矩阵是否是 Toeplitz 矩阵?
如果从左上角到右下角的每条对角线都具有相同的元素,则矩阵是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
热门推荐
1 训练狗狗基础
2 怎么训练狗狗笑
3 怎么训练狗狗卧下
4 上海狗狗训练基地
5 训练狗狗排便
6 狗狗基础训练
8 如何训练狗狗装死
9 狗狗站姿训练
10 狗狗训练用品
11 训练狗狗吧
12 狗狗要怎么训练
13 怎么训练狗狗玩球
14 训练狗狗唤回
15 狗狗训练躺下
16 狗狗训练指令
17 狗狗的训练技巧
18 狗狗不好训练