File tree 1 file changed +33
-0
lines changed
1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -1057,6 +1057,39 @@ class Solution:
1057
1057
return len (tails)
1058
1058
```
1059
1059
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
+
1060
1093
## 309. 最佳买卖股票时机含冷冻期
1061
1094
1062
1095
[ 原题链接] ( https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ )
You can’t perform that action at this time.
0 commit comments