Skip to content

Commit a15de9e

Browse files
committed
Create 1572-matrix-diagonal-sum.js
1 parent dab33ab commit a15de9e

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number[][]} mat
3+
* @return {number}
4+
*/
5+
var diagonalSum = function (mat) {
6+
let sum = 0; // initialize sum to zero
7+
let n = mat.length - 1; // initialize n to mat length - 1
8+
for (let i = 0; i <= n; i++) { // loop through to 0 to n
9+
sum += mat[i][i]; // add mat[i][i] to sum
10+
if (i !== (n - i)) { // if i not equal to n - i then add mat[i][n - i] to sum
11+
sum += mat[i][n - i];
12+
}
13+
}
14+
return sum; // return sum;
15+
};

0 commit comments

Comments
 (0)