Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Matrix Diagonal Sum

// 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]);
    }
}
Comment

sum of two diagonals in matrix

### 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
Comment

PREVIOUS NEXT
Code Example
Python :: &quot;not equal to&quot; python symbol 
Python :: Generators 
Python :: why does async def not work python 
Python :: django admin link column display links 
Python :: how to put quotes in string python 
Python :: matplotlib share colorbar 
Python :: integer to binary python 16 bit 
Python :: Filter by len() 
Python :: image.get p5 
Python :: subtract 2 datetime objects django 
Python :: Another example: using a colorbar to show bar height 
Python :: Insert Multiple Images to Excel with Python 
Python :: multivariate classification python 
Python :: add a third dimension matrix dataset python 
Python :: how to create a leaderboard on python 3.8.1 
Python :: Jhoom.In 
Python :: spyder - identation 
Python :: train chatterbot using yml 
Python :: url namespaces for django rest router urls 
Python :: how many perfect squared lie between 1 and 400 
Python :: fibonacci sequence python 2.7 
Python :: JET token authentication in Django UTC 
Python :: remove exponent pandas plot 
Python :: how to make a password square multicolor square spiral python 
Python :: Symbol to make things not equeal to something in python 
Python :: mechanize python #5 
Python :: check firebase email 
Python :: compressed list 
Python :: Group by date (day, month, year) 
Python :: how to use ci variables in python robot 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =