forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_1572.java
30 lines (28 loc) · 856 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
package com.fishercoder.solutions;
import java.util.HashSet;
import java.util.Set;
public class _1572 {
public static class Solution1 {
public int diagonalSum(int[][] mat) {
int m = mat.length;
Set<Integer> added = new HashSet<>();
int sum = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
if (i == j) {
added.add(i * m + j);
sum += mat[i][j];
}
}
}
for (int i = 0; i < m; i++) {
for (int j = m - 1; j >= 0; j--) {
if (i + j == m - 1 && added.add(i * m + j)) {
sum += mat[i][j];
}
}
}
return sum;
}
}
}