1572. Matrix Diagonal Sum
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 84% Topics: Array, Matrix
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
class Solution(object):
def diagonalSum(self, mat):
"""
:type mat: List[List[int]]
:rtype: int
"""
return sum(mat[i][i]+mat[~i][i] for i in xrange(len(mat))) - (mat[len(mat)//2][len(mat)//2] if len(mat)%2 == 1 else 0)
Solution from kamyu104/LeetCode-Solutions · MIT