carloscn / structstudy

Leetcode daily trainning by using C/C++/RUST programming.
4 stars 1 forks source link

leetcode1480:一维数组的动态和(running-sum-of-1d-array) #235

Open carloscn opened 1 year ago

carloscn commented 1 year ago

问题描述

给你一个数组 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]  

提示:

1 <= nums.length <= 1000 -10^6 <= nums[i] <= 10^6

来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/running-sum-of-1d-array

carloscn commented 1 year ago

问题分析

pub fn running_sum(nums: Vec<i32>) -> Vec<i32>
{
    if nums.len() < 1 {
        return nums;
    }

    let mut ret:Vec<i32> = vec![];

    for i in 0..nums.len() {
        let mut e = 0;
        for j in 0..i + 1 {
            e += nums[j];
        }
        ret.push(e);
    }

    return ret;
}
carloscn commented 1 year ago

code

https://review.gerrithub.io/c/carloscn/structstudy/+/554677 https://github.com/carloscn/structstudy/commit/06984d0f31623307b33e189ee325caf504f45c12