“jumlah elemen diagonal dari python matriks tanpa numpy” Kode Jawaban

Matrix Diagonal Sum 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

jumlah elemen diagonal dari python matriks tanpa numpy

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]
Mayur Rewatkar

Jawaban yang mirip dengan “jumlah elemen diagonal dari python matriks tanpa numpy”

Pertanyaan yang mirip dengan “jumlah elemen diagonal dari python matriks tanpa numpy”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya