Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
describe
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:
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4]Copy the code
Example 2:
Input: nums = [1,1,1,1,1] Output: [1,2,3,4,5] Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1, 1+1+1+1+1]Copy the code
Example 3:
Input: nums = [3,1,2,10,1]
Output: [3,4,6,16,17]
Copy the code
Note:
1 <= nums.length <= 1000
-10^6 <= nums[i] <= 10^6
Copy the code
parsing
RunningSum [I] = sum(nums[0]… Nums [I]), i.e. the sum of all elements from position 0 to position I, is the sum of the elements at position I.
answer
class Solution(object): def runningSum(self, nums): “”” :type nums: List[int] :rtype: List[int] “”” for i in range(1,len(nums)): nums[i] += nums[i-1] return nums
The results
Each node in the linked list has been linked to a single Array in the linked list. Submissions in the Python online list for Running Sum of 1d Array.Copy the code
parsing
For the same idea, just use Python’s built-in function sum.
answer
class Solution(object):
def runningSum(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
result = []
for i in range(len(nums)):
result.append(sum(nums[:i+1]))
return result
Copy the code
The results
Given the submission in the Python online submissions. Memory Usage: 10 ms Submissions in the Python online list for Running Sum of 1d Array.Copy the code
Original link: leetcode.com/problems/ru…
Your support is my biggest motivation