Skip to content

Commit 23c14dc

Browse files
committed
Running Sum of 1d Array
1 parent ff6eea9 commit 23c14dc

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

1480-running-sum-of-1d-array.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
Problem Link: https://leetcode.com/problems/running-sum-of-1d-array/
3+
4+
Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).
5+
Return the running sum of nums.
6+
7+
Example 1:
8+
Input: nums = [1,2,3,4]
9+
Output: [1,3,6,10]
10+
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
11+
12+
Example 2:
13+
Input: nums = [1,1,1,1,1]
14+
Output: [1,2,3,4,5]
15+
Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].
16+
17+
Example 3:
18+
Input: nums = [3,1,2,10,1]
19+
Output: [3,4,6,16,17]
20+
21+
Constraints:
22+
1 <= nums.length <= 1000
23+
-10^6 <= nums[i] <= 10^6
24+
"""
25+
class Solution:
26+
def runningSum(self, nums: List[int]) -> List[int]:
27+
for i in range(1, len(nums)):
28+
nums[i] += nums[i-1]
29+
30+
return nums
31+
32+
33+
class Solution1:
34+
def runningSum(self, nums: List[int]) -> List[int]:
35+
cur_sum, res = 0, []
36+
for num in nums:
37+
cur_sum += num
38+
res.append(cur_sum)
39+
return res

0 commit comments

Comments
 (0)