easy to understand Python solution


  • 0
    class NumArray(object):
        def __init__(self, nums):
            self.sums = {}
            l = len(nums)
            if l==0: return
            def get_sums(i,j):
            	self.sums[(i,j)]=self.sums[(0,j-1)]+nums[j]
            	return self.sums[(i,j)]
            self.sums[(0,0)]=nums[0]
            for i in xrange(1,l):
    		    self.sums[(0,i)] = get_sums(0,i)
    
        def sumRange(self, i, j):
            if len(self.sums)==0: return 0
        	if i==0:
        		return self.sums[(0,j)]
        	else:
        		return self.sums[(0,j)]-self.sums[(0,i-1)]
    

Log in to reply
 

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