Two lines of code with Python


  • 0
    Y
    class NumArray(object):
    
        def __init__(self, nums):
            """
            :type nums: List[int]
            """
            self.cumsum = [sum(nums[:i]) for i in range(1,len(nums)+1)]
            
    
        def sumRange(self, i, j):
            """
            :type i: int
            :type j: int
            :rtype: int
            """
            return self.cumsum[j] - self.cumsum[i-1] if i > 0 else self.cumsum[j]

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.