---
# System prepended metadata

title: 1480. Running Sum of 1d Array

---

# 1480. Running Sum of 1d Array

## [題目:](https://leetcode.com/problems/running-sum-of-1d-array/)

將陣列中的每個數做累加、儲存回陣列  

> 測試資料:
> 
> [1,2,3,4]
[1,1,1,1,1]
[3,1,2,10,1]
>
> 輸出:
[1,3,6,10]
[1,2,3,4,5]
[3,4,6,16,17]

## 思路

一次迴圈解決  

```go=
func runningSum(nums []int) []int {
	var current = 0
	var sum []int
	for _, num := range nums{
		current += num
		sum = append(sum, current)
	}
	return sum
}
```

也可不用建立新的array  

```go=
func runningSum(nums []int) []int {
    for i:=1; i<len(nums); i++{
        nums[i] = nums[i-1] + nums[i]
    }
    return nums
}
```
