mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 17:44:10 +08:00
add: leetcode 1572 solution
This commit is contained in:
@ -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]
|
||||
}
|
Reference in New Issue
Block a user