Matrix Diagonal Sum LeetCode solution in Python

1
class Solution:
2
def diagonalSum(self, mat: List[List[int]]) -> int:
3

4
n = len(mat)
5

6
mid = n // 2
7

8
summation = 0
9

10
for i in range(n):
11

12
# primary diagonal
13
summation += mat[i][i]
14

15
# secondary diagonal
16
summation += mat[n - 1 - i][i]
17

18
if n % 2 == 1:
19
# remove center element (repeated) on odd side-length case
20
summation -= mat[mid][mid]
21

22
return summation

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0