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 :: matrix diagonal sum leetcode in java 
Python :: find highest value in array python 
Python :: # enumerate 
Python :: how to comment text in python 
Python :: python set python key default 
Python :: appending items to a tuple python 
Python :: django email change sender name 
Python :: how to find length of list python 
Python :: python min key 
Python :: function in the input function python 
Python :: trim string to max length python 
Python :: join python documentation 
Python :: uppercase python 
Python :: python update header row 
Python :: ImportError: cannot import name include 
Python :: write string python 
Python :: check audio playing on windows python 
Python :: python check if key exist in json 
Python :: hist pandas 
Python :: find & replace in csv file 
Python :: from pandas to dictionary 
Python :: pandas split column into multiple columns 
Python :: rearrange columns pandas 
Python :: nltk 
Python :: len of iterator python 
Python :: Python how to use __mul__ 
Python :: python json check if key exist 
Python :: execute command in python 
Python :: df length 
Python :: create python dataframe 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =