当前位置:网站首页>力扣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
边栏推荐
- You don't have to run away to delete the library! Detailed MySQL data recovery
- 283. Moving zero-c and language assisted array method
- 迷失在Mysql的锁世界
- 抖音实战~评论数量同步更新
- Use of class methods and class variables
- [advanced C language] array & pointer & array written test questions
- i. Mx6ull driver development | 24 - platform based driver model lights LED
- Open3d surface normal vector calculation
- 玩转gRPC—深入概念与原理
- Bookmark
猜你喜欢
随机推荐
赋能数字经济 福昕软件出席金砖国家可持续发展高层论坛
Why should garment enterprises talk about informatization?
Rotary transformer string judgment
Case sharing | integrated construction of data operation and maintenance in the financial industry
复数在数论、几何中的用途 - 曹则贤
KDD2022 | 什么特征进行交互才是有效的?
玩转gRPC—深入概念与原理
《命令行上的数据科学第二版》校对活动重新启动
Representation of confidence interval
Keep on fighting! The city chain technology digital summit was grandly held in Chongqing
智洋创新与华为签署合作协议,共同推进昇腾AI产业持续发展
服务线上治理
Learning breakout 3 - about energy
置信区间的画法
HDU - 2859 Phalanx(DP)
WebGIS framework -- kalrry
哈希表(Hash Tabel)
# 2156. Find the substring of the given hash value - post order traversal
MySQL存储数据加密
Exclusive interview of open source summer | new committer Xie Qijun of Apache iotdb community








