Skip to content

Commit 2e6bd96

Browse files
1480. Running Sum of 1d Array (List Comprehension)
Difficulty: Easy 53 / 53 test cases passed. Runtime: 48 ms Memory Usage: 14 MB
1 parent 8f4b6a5 commit 2e6bd96

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
Given an array nums. We define a running sum of an array as
3+
runningSum[i] = sum(nums[0]…nums[i]).
4+
5+
Return the running sum of nums.
6+
7+
Example:
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+
Constraints:
13+
- 1 <= nums.length <= 1000
14+
- -10^6 <= nums[i] <= 10^6
15+
"""
16+
#Difficulty: Easy
17+
#53 / 53 test cases passed.
18+
#Runtime: 48 ms
19+
#Memory Usage: 14 MB
20+
21+
#Runtime: 48 ms, faster than 55.37% of Python3 online submissions for Running Sum of 1d Array.
22+
#Memory Usage: 14 MB, less than 100.00% of Python3 online submissions for Running Sum of 1d Array.
23+
24+
class Solution:
25+
def runningSum(self, nums: List[int]) -> List[int]:
26+
return [sum(nums[:i]) for i in range(1, len(nums)+1)]

0 commit comments

Comments
 (0)