Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 58503b4

Browse files
author
Openset
committed
Add: Array Partition I
1 parent adb44b8 commit 58503b4

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
11
package array_partition_i
2+
3+
import "sort"
4+
5+
func arrayPairSum(nums []int) int {
6+
sum := 0
7+
sort.Ints(nums)
8+
for i, v := range nums {
9+
if i&1 == 0 {
10+
sum += v
11+
}
12+
}
13+
return sum
14+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,23 @@
11
package array_partition_i
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input []int
7+
expected int
8+
}
9+
10+
func TestArrayPairSum(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: []int{1, 4, 3, 2},
14+
expected: 4,
15+
},
16+
}
17+
for _, tc := range tests {
18+
output := arrayPairSum(tc.input)
19+
if output != tc.expected {
20+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
21+
}
22+
}
23+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /