当前位置:网站首页>力扣2_1480. 一维数组的动态和
力扣2_1480. 一维数组的动态和
2022-07-04 21:39:00 【上课不要睡觉了】
给你一个数组 nums 。数组「动态和」的计算公式为:runningSum[i] = sum(nums[0]…nums[i]) 。
请返回 nums 的动态和。
示例 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]
来源:力扣(LeetCode)
Java解法
class Solution {
public int[] runningSum(int[] nums) {
int n = nums.length;
for (int i = 1; i < n; i++) {
nums[i] += nums[i - 1];
//从第二位起每一位为前面的累加
}
return nums;
}
}
Python解法(思路和Java的相同)
class Solution:
def runningSum(self, nums: List[int]) -> List[int]:
n = len(nums)
for i in range(1, n):
nums[i] += nums[i - 1]
return nums
边栏推荐
猜你喜欢

Why do you have to be familiar with industry and enterprise business when doing Bi development?

超详细教程,一文入门Istio架构原理及实战应用

Bizchart+slider to realize grouping histogram

输入的查询SQL语句,是如何执行的?

Analyzing the maker space contained in steam Education

Analysis of maker education technology in the Internet Era

Shutter textfield example

从RepVgg到MobileOne,含mobileone的代码

i. Mx6ull driver development | 24 - platform based driver model lights LED

抖音实战~评论数量同步更新
随机推荐
Interview question 01.08 Zero matrix
Delphi soap WebService server-side multiple soapdatamodules implement the same interface method, interface inheritance
抖音实战~评论数量同步更新
面试题 01.01. 判定字符是否唯一
复数在数论、几何中的用途 - 曹则贤
开源之夏专访|Apache IoTDB社区 新晋Committer谢其骏
服装企业为什么要谈信息化?
Redis 排查大 key 的3种方法,优化必备
i.MX6ULL驱动开发 | 24 - 基于platform平台驱动模型点亮LED
Golang interview finishing three resumes how to write
Hash table
How to remove the black dot in front of the title in word document
MongoDB聚合操作总结
机器人相关课程考核材料归档实施细则2022版本
You don't have to run away to delete the library! Detailed MySQL data recovery
Acwing 2022 daily question
# 2156. 查找给定哈希值的子串-后序遍历
挖财学院股票开户安全吗?开户只能在挖财开户嘛?
电话加密,中间4为****代替
服务线上治理