-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmethod_1.py
More file actions
51 lines (41 loc) · 1.19 KB
/
Copy pathmethod_1.py
File metadata and controls
51 lines (41 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/12/1 16:53
# @Author : cancan
# @File : method_1.py
# @Function : 矩阵对角线元素的和
"""
给你一个正方形矩阵 mat,请你返回矩阵对角线元素的和。
请你返回在矩阵主对角线上的元素和副对角线上且不在主对角线上元素的和。
示例 1:
输入:mat = [[1,2,3],
[4,5,6],
[7,8,9]]
输出:25
解释:对角线的和为:1 + 5 + 9 + 3 + 7 = 25
请注意,元素 mat[1][1] = 5 只会被计算一次。
示例 2:
输入:mat = [[1,1,1,1],
[1,1,1,1],
[1,1,1,1],
[1,1,1,1]]
输出:8
示例 3:
输入:mat = [[5]]
输出:5
提示:
- n == mat.length == mat[i].length
- 1 <= n <= 100
- 1 <= mat[i][j] <= 100
"""
from typing import List
class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
ans = 0
l = len(mat)
for i in range(l // 2 if l % 2 == 0 else l // 2 + 1):
if i == l - i - 1 and l % 2 != 0:
ans += mat[i][i]
else:
ans += mat[i][i] + mat[i][l - i - 1] + mat[l - i - 1][i] + mat[l - i - 1][l - i - 1]
return ans