📜  矩阵对角线和 - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:18.349000             🧑  作者: Mango

代码示例1
// Problem Link : https://leetcode.com/problems/matrix-diagonal-sum/

class Solution {
    public int diagonalSum(int[][] mat) {
        int n = mat.length;
        int principal = 0, secondary = 0;
        for (int i = 0; i < n; i++) {
            principal += mat[i][i];
            secondary += mat[i][n - i - 1];
        }
        return n%2 == 0 ? (principal + secondary) : (principal + secondary - mat[n/2][n/2]);
    }
}