Created
June 24, 2020 20:57
-
-
Save liyunrui/9de07a2830f1f8457f982194e9154ba5 to your computer and use it in GitHub Desktop.
leetcoce-range sum query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NumArray: | |
def __init__(self, nums: List[int]): | |
self._prefix_sum = [0 for _ in range(len(nums))] | |
if len(nums) != 0: | |
self._prefix_sum[0] = nums[0] | |
for i in range(1, len(nums)): | |
self._prefix_sum[i] = self._prefix_sum[i-1] + nums[i] | |
def sumRange(self, i: int, j: int) -> int: | |
if i == 0: | |
return self._prefix_sum[j] | |
return self._prefix_sum[j] - self._prefix_sum[i-1] | |
# Your NumArray object will be instantiated and called as such: | |
# obj = NumArray(nums) | |
# param_1 = obj.sumRange(i,j) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment