Easy to Understand C++
I use a for loop to sum the primary and secondary diagonals, and if the both share a common element, it will be subtracted from the ans. Upvote if you like this
I use a for loop to sum the primary and secondary diagonals, and if the both share a common element, it will be subtracted from the ans. Upvote if you like this!\n\nRuntime: 25 ms, faster than 37.29% of C++ online submissions for Matrix Diagonal Sum.\nMemory Usage: 11.4 MB, less than 11.19% of C++ online submissions for Matrix Diagonal Sum.\n\n```\nclass Solution {\npublic:\n    int diagonalSum(vector<vector<int>>& mat) {\n        int n = mat.size(), p = 0, s = 0, ans;\n        for(int i = 0; i < n; i++){\n            p += mat[i][i];\n            s += mat[n-i-1][i];\n        }\n        ans = p + s;\n        if(n%2 != 0){\n            ans -= mat[n/2][n/2];\n        }\n        return ans;\n    }\n};\n```
['C']