Skip to content

1480. Running Sum of 1d Array 👍

  • Time: $O(n)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
 public:
  vector<int> runningSum(vector<int>& nums) {
    vector<int> ans;
    int sum = 0;
    for (const int num : nums)
      ans.push_back(sum += num);
    return ans;
  }
};
1
2
3
4
5
6
7
8
9
class Solution {
  public int[] runningSum(int[] nums) {
    int[] ans = new int[nums.length];
    int sum = 0;
    for (int i = 0; i < nums.length; ++i)
      ans[i] = sum += nums[i];
    return ans;
  }
}
1
2
3
class Solution:
  def runningSum(self, nums: list[int]) -> list[int]:
    return itertools.accumulate(nums)