[LeetCode]#1480. Running Sum of 1d Array

Fatboy Slim
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:

  1. give tmp=0
  2. Use for loop and tmp=tmp+i
  3. 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:

--

--

Fatboy Slim
Fatboy Slim

Written by Fatboy Slim

Interesting in any computer science.

No responses yet