根据上述结论,我们可以遍历整个矩阵,如果当前坐标 (i, j) 满足 i = j 或者 i + j = n - 1 则表示该位置一定在对角线上,则把当前的数字加入到答案之中。
代码
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution { public: intdiagonalSum(vector<vector<int>>& mat){ int n = mat.size(), sum = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i == j || i + j == n - 1) { sum += mat[i][j]; } } } return sum; } };
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11
intdiagonalSum(int** mat, int matSize, int* matColSize) { int n = matSize, sum = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i == j || i + j == n - 1) { sum += mat[i][j]; } } } return sum; }
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { publicintdiagonalSum(int[][] mat) { intn= mat.length, sum = 0; for (inti=0; i < n; ++i) { for (intj=0; j < n; ++j) { if (i == j || i + j == n - 1) { sum += mat[i][j]; } } } return sum; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13
publicclassSolution { publicintDiagonalSum(int[][] mat) { int n = mat.Length, sum = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i == j || i + j == n - 1) { sum += mat[i][j]; } } } return sum; } }
[sol1-Python3]
1 2 3 4 5
classSolution: defdiagonalSum(self, mat: List[List[int]]) -> int: n = len(mat) returnsum(mat[i][j] for i inrange(n) for j inrange(n) \ if i == j or i + j == n - 1)
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12
var diagonalSum = function(mat) { const n = mat.length; let sum = 0; for (let i = 0; i < n; ++i) { for (let j = 0; j < n; ++j) { if (i == j || i + j == n - 1) { sum += mat[i][j]; } } } return sum; };
[sol1-Go]
1 2 3 4 5 6 7 8 9 10 11 12 13
funcdiagonalSum(mat [][]int)int { n := len(mat) sum := 0 for i := 0; i < n; i += 1 { for j := 0; j < n; j += 1 { if i == j || i + j == n - 1 { sum += mat[i][j] } } } return sum }
复杂度分析
时间复杂度:O(n^2),其中 n 是矩阵 mat 的行数。
空间复杂度:O(1)。
方法二:枚举对角线元素
思路与算法
逐行遍历,记当前的行号为 i,则当前行中处于对角线的元素为: 坐标 (i, i) 和坐标 (i, n - i - 1),因此我们把 (i, i) 与 (i, n - i - 1) 处的数字加入到答案中。 如果 n 是奇数的话,则主对角线与副对角线存在交点 (\lfloor \dfrac{n}{2} \rfloor,\lfloor \dfrac{n}{2} \rfloor),该点会被计算两次。所以当 n 为奇数的时候,需要减掉交点处的值。
代码
[sol2-C++]
1 2 3 4 5 6 7 8 9 10
classSolution { public: intdiagonalSum(vector<vector<int>>& mat){ int n = mat.size(), sum = 0, mid = n / 2; for (int i = 0; i < n; ++i) { sum += mat[i][i] + mat[i][n - 1 - i]; } return sum - mat[mid][mid] * (n & 1); } };
[sol2-C]
1 2 3 4 5 6 7
intdiagonalSum(int** mat, int matSize, int* matColSize) { int n = matSize, sum = 0, mid = n / 2; for (int i = 0; i < n; ++i) { sum += mat[i][i] + mat[i][n - 1 - i]; } return sum - mat[mid][mid] * (n & 1); }
[sol2-Java]
1 2 3 4 5 6 7 8 9
classSolution { publicintdiagonalSum(int[][] mat) { intn= mat.length, sum = 0, mid = n / 2; for (inti=0; i < n; ++i) { sum += mat[i][i] + mat[i][n - 1 - i]; } return sum - mat[mid][mid] * (n & 1); } }
[sol2-C#]
1 2 3 4 5 6 7 8 9
publicclassSolution { publicintDiagonalSum(int[][] mat) { int n = mat.Length, sum = 0, mid = n / 2; for (int i = 0; i < n; ++i) { sum += mat[i][i] + mat[i][n - 1 - i]; } return sum - mat[mid][mid] * (n & 1); } }
[sol2-Python3]
1 2 3 4 5 6 7 8
classSolution: defdiagonalSum(self, mat: List[List[int]]) -> int: n = len(mat) total = 0 mid = n // 2 for i inrange(n): total += mat[i][i] + mat[i][n - 1 - i] return total - mat[mid][mid] * (n & 1)
[sol2-JavaScript]
1 2 3 4 5 6 7 8
var diagonalSum = function(mat) { const n = mat.length, mid = Math.floor(n / 2); let sum = 0; for (let i = 0; i < n; ++i) { sum += mat[i][i] + mat[i][n - 1 - i]; } return sum - mat[mid][mid] * (n & 1); };
[sol2-Go]
1 2 3 4 5 6 7 8 9 10 11 12
funcdiagonalSum(mat [][]int)int { n := len(mat) sum := 0 mid := n / 2 for i := 0; i < n; i += 1 { sum += mat[i][i] + mat[i][n - 1 - i] } if n & 1 == 1 { sum -= mat[mid][mid] } return sum }