Posted on 2023-05-08 14:48
Uriel 閱讀(46)
評論(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