-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
1480_running_sum_of_1d_array/running_sum_of_1d_array_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |