Home Leetcode 1480 Running Sum of 1d Array
Post
Cancel

Leetcode 1480 Running Sum of 1d Array

Running Sum of 1d Array problem results

Running Sum of 1d Array

Result

First Blood

My first solution is so straightforward, that I created a new vector to save the running sum

1
2
3
4
5
6
7
8
9
10
11
12
13
{
    vector<int> runningSum(vector<int>& nums) 
    {
        vector<int> runningSum(nums.size());
        runningSum[0] = nums[0];
        for(int i = 1; i < nums.size(); i++)
        {
            runningSum[i] = runningSum[i-1] + nums[i];
        }
        
        return runningSum;
    }
}

Double Kill

It could be better, I can return the result with the input array.

1
2
3
4
5
6
7
8
9
10
11
12
{
    vector<int> runningSum(vector<int>& nums) 
    {
       
        for(int i = 1; i < nums.size(); i++)
        {
            nums[i] += nums[i-1];
        }
        
        return nums;
    }
}
This post is licensed under CC BY 4.0 by the author.
Trending Tags