Skip to content

Commit e04f841

Browse files
authored
Create 1572-matrix-diagonal-sum.java
1 parent d068e08 commit e04f841

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

java/1572-matrix-diagonal-sum.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int diagonalSum(int[][] mat) {
3+
int res = 0;
4+
int n = mat.length;
5+
6+
for (int i = 0; i < n; i++) {
7+
res += mat[i][i];
8+
res += mat[i][n - 1 - i];
9+
}
10+
11+
return res - (n % 2 == 1 ? mat[n / 2][n / 2] : 0);
12+
}
13+
}

0 commit comments

Comments
 (0)