Skip to content

Instantly share code, notes, and snippets.

@Mooophy
Last active November 11, 2015 06:51
Show Gist options
  • Select an option

  • Save Mooophy/05fe62bb071dbcbc0192 to your computer and use it in GitHub Desktop.

Select an option

Save Mooophy/05fe62bb071dbcbc0192 to your computer and use it in GitHub Desktop.
# from https://leetcode.com/problems/range-sum-query-immutable/
class NumArray(object):
def __init__(self, nums):
self.sums = []
sum = 0
for num in nums:
sum += num
self.sums.append(sum)
def sumRange(self, i, j):
return self.sums[j] if i == 0 else self.sums[j] - self.sums[i - 1]
# Your NumArray object will be instantiated and called as such:
# numArray = NumArray(nums)
# numArray.sumRange(0, 1)
# numArray.sumRange(1, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment