当前位置:网站首页>力扣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
边栏推荐
- Telephone encryption, middle 4 is replaced by * * * *
- [advanced C language] array & pointer & array written test questions
- Cloudcompare & open3d DBSCAN clustering (non plug-in)
- How was MP3 born?
- gtest从一无所知到熟练使用(2)什么是测试夹具/装置(test fixture)
- 283. Moving zero-c and language assisted array method
- 迷失在Mysql的锁世界
- Kubedm initialization error: [error cri]: container runtime is not running
- 凭借了这份 pdf,最终拿到了阿里,字节,百度等八家大厂 offer
- Representation of confidence interval
猜你喜欢

El tree combined with El table, tree adding and modifying operations

Case sharing | integrated construction of data operation and maintenance in the financial industry

如何借助自动化工具落地DevOps

ArcGIS 10.2.2 | solution to the failure of ArcGIS license server to start

迷失在Mysql的锁世界

i.MX6ULL驱动开发 | 24 - 基于platform平台驱动模型点亮LED

gtest从一无所知到熟练使用(3)什么是test suite和test case

Machine learning notes mutual information

CloudCompare&Open3D DBSCAN聚类(非插件式)

并发优化总结
随机推荐
删库不必跑路!详解 MySQL 数据恢复
Open3d surface normal vector calculation
解决异步接口慢导致的数据错乱问题
PostgreSQL基本结构——表
开户哪家券商比较好?网上开户安全吗
Redis03 - network configuration and heartbeat mechanism of redis
What is the stock account opening process? Is it safe to use flush mobile stock trading software?
Shutter WebView example
Why should garment enterprises talk about informatization?
湘江鲲鹏加入昇腾万里伙伴计划,与华为续写合作新篇章
The drawing method of side-by-side diagram, multi row and multi column
HDU - 2859 Phalanx(DP)
Caduceus从未停止创新,去中心化边缘渲染技术让元宇宙不再遥远
Operation of adding material schedule in SolidWorks drawing
Super detailed tutorial, an introduction to istio Architecture Principle and practical application
广电五舟与华为签署合作协议,共同推进昇腾AI产业持续发展
AcWing 2022 每日一题
可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
力扣3_383. 赎金信
Bookmark