当前位置:网站首页>力扣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
边栏推荐
- What is business intelligence (BI), just look at this article is enough
- 机器人相关课程考核材料归档实施细则2022版本
- 传智教育|如何转行互联网高薪岗位之一的软件测试?(附软件测试学习路线图)
- How much is the minimum stock account opening commission? Is it safe to open an account online
- HUAWEI nova 10系列发布 华为应用市场筑牢应用安全防火墙
- 输入的查询SQL语句,是如何执行的?
- 网上开户哪家证券公司佣金最低,我要开户,网上开户安全吗
- [optimtool.unconstrained] unconstrained optimization toolbox
- Telephone encryption, middle 4 is replaced by * * * *
- What is the stock account opening process? Is it safe to use flush mobile stock trading software?
猜你喜欢
Exclusive interview of open source summer | new committer Xie Qijun of Apache iotdb community
NAACL-22 | 在基于Prompt的文本生成任务上引入迁移学习的设置
超详细教程,一文入门Istio架构原理及实战应用
使用 BlocConsumer 同时构建响应式组件和监听状态
应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设
What is business intelligence (BI), just look at this article is enough
el-tree结合el-table,树形添加修改操作
湘江鲲鹏加入昇腾万里伙伴计划,与华为续写合作新篇章
一文掌握数仓中auto analyze的使用
VS2019 C# release下断点调试
随机推荐
How was MP3 born?
Cloudcompare & open3d DBSCAN clustering (non plug-in)
Kubedm initialization error: [error cri]: container runtime is not running
Cadre WebGIS - kalrry
Analysis of maker education technology in the Internet Era
bizchart+slider实现分组柱状图
输入的查询SQL语句,是如何执行的?
玩转gRPC—深入概念与原理
[advanced C language] array & pointer & array written test questions
1807. Replace the parentheses in the string
[early knowledge of activities] list of recent activities of livevideostack
HDU - 2859 Phalanx(DP)
gtest从一无所知到熟练运用(1)gtest安装
Is it safe to open an account in the stock of Caicai college? Can you only open an account by digging money?
湘江鲲鹏加入昇腾万里伙伴计划,与华为续写合作新篇章
VIM from dislike to dependence (23) -- the last gossip
WebGIS框架---kalrry
New intersectionobserver usage notes
The drawing method of side-by-side diagram, multi row and multi column
# 2156. 查找给定哈希值的子串-后序遍历