Given an array of integers, We have to return the running sum of that array.
Suppose, we have an array of integers like [1, 2, 3]. As per the problem, the result of this array will be [1, 3, 6]. We define a running sum of an array as arr[i] = sum(arr…arr[i]).
In each position of the array, we have to store the sum of arr to arr[i] instead of that position value. So, the array will work like [1, 1+2, 1+2+3].
To solve this problem, we will loop through the array from index number one, not zero. Because each time we have to check the previous element. Each time we add the current element value with our previous element value and store it in the current position.
After the loop, we will return the array.
My code will explain better than me. Let see the solution below.
class Solution: def runningSum(self, nums: List[int]) -> List[int]: for i in range(1, len(nums)): nums[i] = nums[i] + nums[i-1] return nums
If you submit the code in LeetCode, it will be accepted.