Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

matrix diagonal sum 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]
Comment

matrix diagonal sum 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]);
    }
}
Comment

PREVIOUS NEXT
Code Example
Python :: how to use if else in python 
Python :: how to find and remove certain characters from text string in python 
Python :: python enumerate 
Python :: how to comment code in python 
Python :: python how to drop columns from dataframe 
Python :: boxplot python 
Python :: python multiply string 
Python :: python merge strings 
Python :: Python script for computing descriptive statistics 
Python :: pandas series remove element by index 
Python :: binary to decimal in python without inbuilt function 
Python :: is in array python 
Python :: confusion matrix with seaborn heatmap 
Python :: WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. 
Python :: django admin readonly models 
Python :: write python 
Python :: how to add a column with more rows to a dataframe 
Python :: python tutorial pdf 
Python :: how to negate a boolean python 
Python :: how to show installed tkinter fonts 
Python :: python str of list 
Python :: python max value in list 
Python :: python check if ip is up or down 
Python :: python get index of substring in liast 
Python :: exit code python 
Python :: Python Changing a Tuple 
Python :: How to Send WhatsApp API using python 
Python :: empty array python 
Python :: count element in set python 
Python :: pop element from heap python 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =