LeetCode - Algorithms - 1572. Matrix Diagonal Sum

Easy indeed! I can do it on leetcode editor without IDE debug.

Problem

1572. Matrix Diagonal Sum

Java

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public int diagonalSum(int[][] mat) {
final int N = mat.length;
int sum = 0;
for(int i=0;i<N;i++) {
sum += mat[i][i];
if ((N-1)!=2*i)
sum += mat[i][N-1-i];
}
return sum;
}
}

Submission Detail

  • 113 / 113 test cases passed.
  • Runtime: 0 ms, faster than 100.00% of Java online submissions for Matrix Diagonal Sum.
  • Memory Usage: 39.5 MB, less than 16.30% of Java online submissions for Matrix Diagonal Sum.