​problem​

​1572. Matrix Diagonal Sum​

hint

There will be overlap of elements in the primary and secondary
diagonals if and only if the length of the matrix is odd, which

Synopsis

Accumulate both diagonal sums simultaneously.
Subtract the middle sum once if

solution#1: 

code:

 

注意,如何求解方阵的对角线元素之和。

solution#2: one Liner Solutions;

code

 

参考

1. ​​leetcode_easy_array_1572. Matrix Diagonal Sum​​;

2. ​​leetcode_discussion​​;