# Running Sum of 1d Array

{% embed url="<https://leetcode.com/problems/running-sum-of-1d-array/>" %}

## Solution 1 (Java)

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

A little teaser for DP.

## Solution 2 (Java)

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

The separate array is not actually needed. We can do the adding process in place. The complexities stay the same though.

* Time complexity: `O(n)`
* Space complexity: `O(1)`


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.yushunchen.com/algo/array/running-sum-of-1d-array.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
