Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e7a3d3d

Browse files
🐱(dynamic): 303. 区域和检索 - 数组不可变
1 parent 3e4916b commit e7a3d3d

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

‎docs/algorithm/dynamic/README.md‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,39 @@ class Solution:
10571057
return len(tails)
10581058
```
10591059

1060+
## 303. 区域和检索 - 数组不可变
1061+
1062+
[原题链接](https://leetcode-cn.com/problems/range-sum-query-immutable/)
1063+
1064+
### 思路
1065+
1066+
动态规划前缀和。
1067+
1068+
```python
1069+
class NumArray:
1070+
1071+
def __init__(self, nums: List[int]):
1072+
length = len(nums)
1073+
self.sum_nums = [0 for _ in range(length)]
1074+
for i in range(length):
1075+
if i == 0:
1076+
self.sum_nums[i] = nums[i]
1077+
else:
1078+
self.sum_nums[i] = self.sum_nums[i - 1] + nums[i]
1079+
1080+
1081+
def sumRange(self, i: int, j: int) -> int:
1082+
if i == 0:
1083+
return self.sum_nums[j]
1084+
else:
1085+
return self.sum_nums[j] - self.sum_nums[i - 1]
1086+
1087+
1088+
# Your NumArray object will be instantiated and called as such:
1089+
# obj = NumArray(nums)
1090+
# param_1 = obj.sumRange(i,j)
1091+
```
1092+
10601093
## 309. 最佳买卖股票时机含冷冻期
10611094

10621095
[原题链接](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /