LeetCode Brush Questions 1480. Dynamic Sum of One-Dimensional Array

LeetCode Brush Questions 1480. Dynamic Sum of One-Dimensional Array

I don't know where I am going, but I am already on my way!
Time is hurried, although I have never met, but I met Yusi, it is really a great fate, thank you for your visit!
  • Subject :
    Here is an array for you nums. Array formula "and dynamic" as follows: runningSum[i] = sum(nums[0]…nums[i]). Please return to numsthe dynamic and.
  • Example :
示例 1 :
输入:nums = [1,2,3,4]
输出:[1,3,6,10]
解释:动态和计算过程为 [1, 1+2, 1+2+3, 1+2+3+4] 。
示例 2 :
输入:nums = [1,1,1,1,1]
输出:[1,2,3,4,5]
解释:动态和计算过程为 [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1] 。
示例 3:
输入:nums = [3,1,2,10,1]
输出:[3,4,6,16,17]
  • Tips :
    • 1 <= nums.length <= 1000
    • -10^6 <= nums[i] <= 10^6
  • Code:
class Solution:
    def runningSum(self, nums: List[int]) -> List[int]:
        result = [0]
        for i in range(len(nums)):
            result.append(result[-1] + nums[i])
        return result[1:]
# 执行用时:36 ms, 在所有 Python3 提交中击败了91.69%的用户
# 内存消耗:13.9 MB, 在所有 Python3 提交中击败了5.30%的用户
  • Algorithm description: It is
    not possible to sum up from the beginning every time, resultadd new elements to the last result used , and add it to resultit.

Guess you like

Origin blog.csdn.net/qq_34331113/article/details/108365188