Simple python solution : use dict

  • 6
    class NumArray(object):
        def __init__(self, nums):
            initialize your data structure here.
            :type nums: List[int]
            self.dc = {-1:0}
            for i,v in enumerate(nums):
                self.dc[i] = self.dc[i-1] + v
        def sumRange(self, i, j):
            sum of elements nums[i..j], inclusive.
            :type i: int
            :type j: int
            :rtype: int
            return self.dc[j]-self.dc[i-1]

Log in to reply

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