LeetCode 筆記 - 1480. Running Sum of 1d Array

題目在此 1480. Running Sum of 1d Array

給定一個數列,請做出 Prefix Sum

解題思維

Prefix Sum 是應用蠻廣的一個演算法
在這題 Easy to understand 的題目可以很單純的來熟悉一下這個演算法

答案裡的每一個 i 代表的是從 nums[0] 連續加到 nums[i] 的結果

程式碼

1
2
3
4
class Solution:
def runningSum(self, nums: List[int]) -> List[int]:
temp = 0
return [(temp := temp + i) for i in nums]

相關文章