Intuition


First I thought about just summing index 1, not 0, with the previous element (e.g.: i - 1). However, the output needs to be an integer array. This is my initial non-working code:

class Solution {
 
    public int[] runningSum(int[] nums) {
        int[] runningSum = 0;
 
        for (int i = 1; i < nums.length; i++) {
            runningSum = nums[i] + nums[i-1];
        }
        return runningSum;
    }
}

Approach


Not much to say, pretty simple.

Complexity


  • Time complexity:

  • Space complexity: (constant)

Code


class Solution {
    public int[] runningSum(int[] nums) {
        int[] res = new int[nums.length];
 
        // the first sum is always the first value
        res[0] = nums[0];
 
        for (int i = 1; i < nums.length; i++) {
            res[i] = nums[i] + res[i-1];
        }
        return res;
    }
}
class Solution {
    public int[] runningSum(int[] nums) {
        for (int i = 1; i < nums.length; i++) {
            nums[i] += nums[i-1];
        }
        return nums;
    }
}