“matrix -diagonale Summe Leetcode” Code-Antworten

matrix -diagonale Summe Leetcode

# Problem Link : https://leetcode.com/problems/matrix-diagonal-sum/

class Solution(object):
    def diagonalSum(self, array):
        """
        :type array: List[List[int]]
        :rtype: int
        """
        n = len(array)
        primary = 0
        secondary = 0;
        for i in range(0, n):
            primary += array[i][i]
            secondary += array[i][n-i-1]
        if (n % 2 == 0): return primary + secondary
        else: return primary + secondary - array[n//2][n//2]
Prabhu Kiran Konda

matrix -diagonale Summe Leetcode

// 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]);
    }
}
Prabhu Kiran Konda

Ähnliche Antworten wie “matrix -diagonale Summe Leetcode”

Fragen ähnlich wie “matrix -diagonale Summe Leetcode”

Weitere verwandte Antworten zu “matrix -diagonale Summe Leetcode” auf Java

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen