-
Notifications
You must be signed in to change notification settings - Fork 0
/
303-1.py
39 lines (31 loc) · 833 Bytes
/
303-1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = ['"wuyadong" <[email protected]>']
class NumArray(object):
def __init__(self, nums):
"""
initialize your data structure here.
:type nums: List[int]
"""
self.n = len(nums)
self.totals = []
total = 0
for num in nums:
total += num
self.totals.append(total)
def sumRange(self, i, j):
"""
sum of elements nums[i..j], inclusive.
:type i: int
:type j: int
:rtype: int
"""
if self.n == 0:
return 0
if i == 0:
return self.totals[j]
else:
return self.totals[j] - self.totals[i-1]
if __name__ == "__main__":
n = NumArray([1, 1, 1, 1])
print n.sumRange(1, 3)