Given a square matrix mat, return the sum of the matrix diagonals.
Only include the sum of all the elements on the primary diagonal and all the elements on the secondary diagonal that are not part of the primary diagonal.
Example 1:
1 | Input: mat = [[1,2,3], |
Example 2:
1 | Input: mat = [[1,1,1,1], |
Example 3:
1 | Input: mat = [[5]] |
Constraints:
1 | n == mat.length == mat[i].length |
Approach
1 | Collect the sum of the primary and the secondary diagonals. Subtract the reapeatedly added center element if the matSize is odd. |
Algorithm
1 | 1. Go through the matrix from the first column to the last column. |
Implementation
1 | int diagonalSum(int** mat, int matSize, int* matColSize){ |