Skip to content

Commit 2e5b3bf

Browse files
authored
Create 1572-matrix-diagonal-sum.js
1 parent b0427fb commit 2e5b3bf

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

1572-matrix-diagonal-sum.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number[][]} mat
3+
* @return {number}
4+
*/
5+
const diagonalSum = function(mat) {
6+
let res = 0, n = mat.length
7+
for(let i = 0; i < n; i++) {
8+
const j = i, ii = i, jj = n - 1 - i
9+
if(j == jj) res += mat[i][j]
10+
else {
11+
res += mat[i][j] + mat[ii][jj]
12+
}
13+
}
14+
15+
return res
16+
};

0 commit comments

Comments
 (0)