“Matrice Somme diagonale” Réponses codées

matrice diagonale somme python

# 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

Matrice Somme diagonale

// 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

somme de deux diagonales dans la matrice

### START FUNCTION
def sum_of_diagonals(matrix):
    diag_1=0
    diag_2=0
    k = matrix.shape[1]-1
    for i in range(matrix.shape[0]):
        diag_1 += matrix[i][i]
        diag_2 += matrix[i][k]
        k -=1
    if matrix.shape[0]%2 != 0:
        mid = int(np.floor(matrix.shape[0]/2))
        sum = diag_1+diag_2-matrix[mid][mid]
    else:
        sum = diag_1+diag_2
    return sum
### END FUNCTION
Africodes

Réponses similaires à “Matrice Somme diagonale”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code