Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int diagonalSum(vector<vector<int>>& mat) {
- int sum=0;
- for(int i=0;i<mat.size();i++)
- {
- for(int j=0;j<mat[i].size();j++)
- {
- if(i==j)
- {
- sum+=mat[i][j];
- }
- }
- }
- int i=0;
- int j=mat.size()-1;
- while(i<mat.size())
- {
- sum+=mat[i][j];
- i++;
- j--;
- }
- if(mat.size()%2!=0)
- {
- int k=mat.size();
- sum-=mat[k/2][k/2];
- }
- return sum;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment