mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
[ADD] 1480 & 1512
This commit is contained in:
@ -0,0 +1,19 @@
|
||||
package leetcode
|
||||
|
||||
func runningSum(nums []int) []int {
|
||||
result := []int{}
|
||||
counter := 0
|
||||
|
||||
for x := 0; x < len(nums); x++ {
|
||||
for y := 0; y < x; y++ {
|
||||
counter += nums[y]
|
||||
}
|
||||
|
||||
val := counter + nums[x]
|
||||
result = append(result, val)
|
||||
|
||||
counter = 0
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1480 struct {
|
||||
para1480
|
||||
ans1480
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1480 struct {
|
||||
nums []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1480 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
func Test_Problem1480(t *testing.T) {
|
||||
|
||||
qs := []question1480{
|
||||
|
||||
{
|
||||
para1480{[]int{1, 2, 3, 4}},
|
||||
ans1480{[]int{1, 2, 6, 10}},
|
||||
},
|
||||
|
||||
{
|
||||
para1480{[]int{1, 1, 1, 1, 1}},
|
||||
ans1480{[]int{1, 2, 3, 4, 5}},
|
||||
},
|
||||
|
||||
{
|
||||
para1480{[]int{3, 1, 2, 10, 1}},
|
||||
ans1480{[]int{3, 4, 6, 16, 17}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1480------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1480, q.para1480
|
||||
fmt.Printf("【input】:%v 【output】:%v \n", p, runningSum(p.nums))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
59
leetcode/1480.Running-Sum-of-1d-Array/README.md
Normal file
59
leetcode/1480.Running-Sum-of-1d-Array/README.md
Normal file
@ -0,0 +1,59 @@
|
||||
# [1480. Running Sum of 1d Array](https://leetcode.com/problems/running-sum-of-1d-array/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given an array `nums`. We define a running sum of an array as `runningSum[i] = sum(nums[0]…nums[i])`.
|
||||
|
||||
Return the running sum of `nums`.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: nums = [1,2,3,4]
|
||||
Output: [1,3,6,10]
|
||||
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
|
||||
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: nums = [1,1,1,1,1]
|
||||
Output: [1,2,3,4,5]
|
||||
Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].
|
||||
|
||||
```
|
||||
|
||||
**Example 3**:
|
||||
|
||||
```
|
||||
Input: nums = [3,1,2,10,1]
|
||||
Output: [3,4,6,16,17]
|
||||
|
||||
```
|
||||
|
||||
**Constraints**:
|
||||
|
||||
- `1 <= nums.length <= 1000`
|
||||
- `-10^6 <= nums[i] <= 10^6`
|
||||
|
||||
```go
|
||||
func runningSum(nums []int) []int {
|
||||
result := []int{}
|
||||
counter := 0
|
||||
|
||||
for x := 0; x < len(nums); x++ {
|
||||
for y := 0; y < x; y++ {
|
||||
counter += nums[y]
|
||||
}
|
||||
|
||||
val := counter + nums[x]
|
||||
result = append(result, val)
|
||||
|
||||
counter = 0
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
```
|
Reference in New Issue
Block a user