1588. Sum of All Odd Length Subarrays
设置子数组长度初始为1,返回值为0,当子数组长度不大于arr长度时,遍历arr,在其中选定范围用sum求和,每次遍历结束都把子数组长度+2,最后返回总和rat。
时间复杂度:不会算,速度还可以 60 ms, faster than 68.58%
空间复杂度:O(1)
class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
subarraylen = 1
rat = 0
while len(arr) >= subarraylen:
for i in range(0, len(arr) - subarraylen + 1):
rat += sum(arr[i:i + subarraylen])
subarraylen += 2
return rat
LeetCode #1588. Sum of All Odd Length Subarrays
原文:https://www.cnblogs.com/RatsCommander/p/14074758.html