- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1572.java
30 lines (28 loc) · 871 Bytes
/
_1572.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
packagecom.fishercoder.solutions.secondthousand;
importjava.util.HashSet;
importjava.util.Set;
publicclass_1572 {
publicstaticclassSolution1 {
publicintdiagonalSum(int[][] mat) {
intm = mat.length;
Set<Integer> added = newHashSet<>();
intsum = 0;
for (inti = 0; i < m; i++) {
for (intj = 0; j < m; j++) {
if (i == j) {
added.add(i * m + j);
sum += mat[i][j];
}
}
}
for (inti = 0; i < m; i++) {
for (intj = m - 1; j >= 0; j--) {
if (i + j == m - 1 && added.add(i * m + j)) {
sum += mat[i][j];
}
}
}
returnsum;
}
}
}