Medium
看到字符组的和,可以想到使用前缀和方法来做,在看数据规模,\(O(n^2)\)以下的时间复杂度应该都可以AC。
以上,AC!
python3
class Solution:
def rangeSum(self, nums: List[int], n: int, left: int, right: int) -> int:
presum = [0] * n
pre = 0
for i,n in enumerate(nums):
presum[i] = pre + n
pre = presum[i]
presum = [0] + presum
temp = []
for i in range(len(presum)):
for j in range(i+1,len(presum)):
temp.append(presum[j]-presum[i])
return sum(sorted(temp)[left-1:right]) % (10**9+7)