鱼C论坛

 找回密码
 立即注册
查看: 1550|回复: 2

[技术交流] 【朱迪的LeetCode刷题笔记】1480. Running Sum of 1d Array #Easy #Python

[复制链接]
发表于 2023-5-23 11:40:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Judie 于 2023-5-23 02:22 编辑

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:
Input: nums = [1,2,3,4]
Output: [1,3,6,10]
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].

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+1].

Example 3:
Input: nums = [3,1,2,10,1]
Output: [3,4,6,16,17]


Constraints:
1 <= nums.length <= 1000
-10^6 <= nums[i] <= 10^6

  1. class Solution(object):
  2.     def runningSum(self, nums):
  3.         """
  4.         :type nums: List[int]
  5.         :rtype: List[int]
  6.         """
复制代码


Judy
  1. class Solution(object):
  2.     def runningSum(self, nums):
  3.         """
  4.         :type nums: List[int]
  5.         :rtype: List[int]
  6.         """
  7.         i = 1
  8.         l = len(nums)
  9.         s = nums[0]
  10.         runningSum = [s]
  11.         while i < l:
  12.             s += nums[i]
  13.             runningSum.append(s)
  14.             i +=1
  15.         return runningSum
  16.         
复制代码

Screen Shot 2023-05-22 at 11.33.59 PM.png

Gray
  1. class Solution(object):
  2.     def runningSum(self, nums):
  3.         """
  4.         :type nums: List[int]
  5.         :rtype: List[int]
  6.         """
  7.         ans = []
  8.         temp = 0
  9.         for x in nums:
  10.             ans.append(temp + x)
  11.             temp = temp + x
  12.         return ans
复制代码

WechatIMG31205.jpeg

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-5-23 19:35:36 | 显示全部楼层
不错,希望更下去
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-5-23 23:55:21 | 显示全部楼层

谢谢 我努力!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-21 14:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表