forked from tcandzq/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRangeSumQueryImmutable.py
More file actions
41 lines (28 loc) · 1003 Bytes
/
RangeSumQueryImmutable.py
File metadata and controls
41 lines (28 loc) · 1003 Bytes
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
40
41
# -*- coding: utf-8 -*-
# @File : RangeSumQueryImmutable.py
# @Date : 2020-02-10
# @Author : tc
"""
题号 303 区域和检索 - 数组不可变
给定一个整数数组 nums,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点。
示例:
给定 nums = [-2, 0, 3, -5, 2, -1],求和函数为 sumRange()
sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3
说明:
你可以假设数组不可变。
会多次调用 sumRange 方法。
参考:https://leetcode-cn.com/problems/range-sum-query-immutable/solution/qian-zhui-he-by-powcai-2/
"""
from typing import List
class NumArray:
def __init__(self, nums: List[int]):
self.dp = [0]
for num in nums:
self.dp.append(self.dp[-1] + num)
def sumRange(self, i: int, j: int) -> int:
return self.dp[j + 1] - self.dp[i]
# Your NumArray object will be instantiated and called as such:
# obj = NumArray(nums)
# param_1 = obj.sumRange(i,j)