1572. Matrix Diagonal Sum Solution

class Solution {
    public int diagonalSum(int[][] mat) {
        int sum = 0;
        for(int i=0;i<mat.length;i++) {
            for(int j=0;j<mat.length;j++) {
                if (i == j) {
                    sum += mat[i][j];
                } else if (i+j == mat.length-1) {
                    sum += mat[i][j];
                }
            }
        }
        return sum;
    }
}

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *