1572. Matrix Diagonal Sum

problem

solution

one pass

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int diagonalSum(vector<vector<int>>& mat) {
int n = mat.size(), idx = 0, ret = 0;
for(int i=0;i<n;++i){
ret+=mat[i][idx];
if(idx != n-1-idx) ret+=mat[i][n-1-idx];
idx++;
}
return ret;
}
};

analysis

  • time complexity O(nm)
  • space complexity O(1)