Skip to content

Commit

Permalink
1480 0ms 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
mutoe committed Sep 20, 2021
1 parent f58a560 commit d13d6d1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
19 changes: 19 additions & 0 deletions 1480_running_sum_of_1d_array/running_sum_of_1d_array.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package running_sum_of_1d_array

// https://leetcode.com/problems/running-sum-of-1d-array

// level: 1
// time: O(n) 0ms 100%
// space: O(n) 2.8M 41.34%

//leetcode submit region begin(Prohibit modification and deletion)
func runningSum(nums []int) []int {
N := len(nums)
sum := make([]int, N+1)
for i := 0; i < N; i++ {
sum[i+1] = sum[i] + nums[i]
}
return sum[1:]
}

//leetcode submit region end(Prohibit modification and deletion)
40 changes: 40 additions & 0 deletions 1480_running_sum_of_1d_array/running_sum_of_1d_array_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package running_sum_of_1d_array

import (
"reflect"
"testing"
)

func Test_runningSum(t *testing.T) {
type args struct {
nums []int
}
tests := []struct {
name string
args args
want []int
}{
{
name: "test1",
args: args{[]int{1, 2, 3, 4}},
want: []int{1, 3, 6, 10},
},
{
name: "test2",
args: args{[]int{1, 1, 1, 1, 1}},
want: []int{1, 2, 3, 4, 5},
},
{
name: "test3",
args: args{[]int{3, 1, 2, 10, 1}},
want: []int{3, 4, 6, 16, 17},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := runningSum(tt.args.nums); !reflect.DeepEqual(got, tt.want) {
t.Errorf("runningSum() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit d13d6d1

Please sign in to comment.