Posted on 2023-05-08 14:48
Uriel 阅读(34)
评论(0) 编辑 收藏 引用 所属分类:
闲来无事重切Leet Code 、
大水题
求二维数组两条对角线元素之和(每个元素只算一次),水题
1 #1572
2 #Runtime: 93 ms (Beats 7.69%)
3 #Memory: 13.7 MB (Beats 15.92%)
4
5 class Solution(object):
6 def diagonalSum(self, mat):
7 """
8 :type mat: List[List[int]]
9 :rtype: int
10 """
11 ans = 0
12 for i in range(min(len(mat), len(mat[0]))):
13 ans += mat[i][i]
14 for i in range(min(len(mat), len(mat[0]))):
15 j = len(mat[0]) - i - 1
16 if i != j:
17 ans += mat[i][j]
18 return ans