当前位置:网站首页>力扣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
边栏推荐
- Shutter textfield example
- Open3D 曲面法向量计算
- Keep on fighting! The city chain technology digital summit was grandly held in Chongqing
- Representation of confidence interval
- # 2156. 查找给定哈希值的子串-后序遍历
- 关系型数据库
- close系统调用分析-性能优化
- 使用 BlocConsumer 同时构建响应式组件和监听状态
- Telephone encryption, middle 4 is replaced by * * * *
- 283. 移动零-c与语言辅助数组法
猜你喜欢

Bizchart+slider to realize grouping histogram

应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设

Redis03 - network configuration and heartbeat mechanism of redis

HUAWEI nova 10系列发布 华为应用市场筑牢应用安全防火墙

能源势动:电力行业的碳中和该如何实现?

Analyzing the maker space contained in steam Education

TCP shakes hands three times and waves four times. Do you really understand?

Redis has three methods for checking big keys, which are necessary for optimization

Shutter textfield example

CloudCompare&Open3D DBSCAN聚类(非插件式)
随机推荐
GTEST from ignorance to skillful use (1) GTEST installation
更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
Open3D 曲面法向量计算
bizchart+slider实现分组柱状图
抖音实战~评论数量同步更新
Cadeus has never stopped innovating. Decentralized edge rendering technology makes the metauniverse no longer far away
并发网络模块化 读书笔记转
Application practice | Shuhai supply chain construction of data center based on Apache Doris
Machine learning notes mutual information
Operation of adding material schedule in SolidWorks drawing
The drawing method of side-by-side diagram, multi row and multi column
开源之夏专访|Apache IoTDB社区 新晋Committer谢其骏
PostgreSQL基本结构——表
从RepVgg到MobileOne,含mobileone的代码
How to implement Devops with automatic tools
力扣98:验证二叉搜索树
[optimtool.unconstrained] unconstrained optimization toolbox
网上开户哪家证券公司佣金最低,我要开户,网上开户安全吗
1807. 替换字符串中的括号内容
B站大量虚拟主播被集体强制退款:收入蒸发,还倒欠B站;乔布斯被追授美国总统自由勋章;Grafana 9 发布|极客头条