1572. Matrix Diagonal Sum
Question
Given a square matrix mat
, return the sum of the matrix diagonals.
Only include the sum of all the elements on the primary diagonal and all the elements on the secondary diagonal that are not part of the primary diagonal.
Solution
双指针记录两个矩阵对应的位置,遍历相加即可。
注意当两个指针指向同一个位置时需要跳过一个和。
Code
1 | class Solution { |
1572. Matrix Diagonal Sum
https://xuanhe95.github.io/2023/05/08/1572-Matrix-Diagonal-Sum/