Last active
November 11, 2015 06:51
-
-
Save Mooophy/05fe62bb071dbcbc0192 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # 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