[LeetCode]#1480. Running Sum of 1d Array
1 min readJun 26, 2020
Environment: Python 3.7
Key technique: for, append
Given an array nums
. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i])
.
Return the running sum of nums
.
Example 1:
Input: nums = [1,2,3,4]
Output: [1,3,6,10]
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
Analysis:
- give tmp=0
- Use for loop and tmp=tmp+i
- Use ans to add tmp for each for loop iteration.
Solution:
class Solution:
def runningSum(self, nums):
ans=[]
tmp=0
for i in nums:
tmp+=i
ans.append(tmp)
return ans
Submissions: