We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a368ada commit a7f901cCopy full SHA for a7f901c
scripts/algorithms/M/Matrix Diagonal Sum/Matrix Diagonal Sum.cpp
@@ -1,13 +1,14 @@
1
+// Runtime: 41 ms (Top 5.11%) | Memory: 11.3 MB (Top 10.91%)
2
class Solution {
3
public:
4
int diagonalSum(vector<vector<int>>& mat) {
5
int n = mat.size() ;
6
int ans = 0 ;
-
7
+
8
for(int i = 0 ; i < n ; i++){
- ans = ans + mat[i][i] + mat[i][n - i - 1] ;
9
+ ans = ans + mat[i][i] + mat[i][n - i - 1] ;
10
}
11
ans = (n & 1) ? ans - mat[n/2][n/2] : ans ;//if n is odd then we have to subtract mat[n/2][n/2] from the ans because we add it twice earlier.
- return ans ;
12
+ return ans ;
13
-};
14
+};
0 commit comments