add: leetcode 1572 solution

This commit is contained in:
tphyhFighting
2021-05-21 16:48:31 +08:00
parent 86370c1ac5
commit 36f3a4c757
3 changed files with 141 additions and 0 deletions

View File

@ -0,0 +1,16 @@
package leetcode
func diagonalSum(mat [][]int) int {
n := len(mat)
ans := 0
for pi := 0; pi < n; pi++ {
ans += mat[pi][pi]
}
for si, sj := n-1, 0; sj < n; si, sj = si-1, sj+1 {
ans += mat[si][sj]
}
if n%2 == 0 {
return ans
}
return ans - mat[n/2][n/2]
}