diff --git a/20201223/main.go b/20201223/main.go new file mode 100644 index 0000000..50778c1 --- /dev/null +++ b/20201223/main.go @@ -0,0 +1,32 @@ +package main + +import "sort" + +func main() { + +} + +func find(target int, candidates []int) [][]int { + l := len(candidates) + var ret [][]int + sort.Ints(candidates) + for k, n := range candidates { + for j := 1; j < l-k-1; j++ { + sum := n + nums := []int{n} + for i := k + j; i < l; i++ { + num := candidates[i] + sum += num + if sum > target { + break + } + nums = append(nums, num) + if sum == target { + ret = append(ret, nums) + break + } + } + } + } + return ret +} diff --git a/20201223/main_test.go b/20201223/main_test.go new file mode 100644 index 0000000..f64d911 --- /dev/null +++ b/20201223/main_test.go @@ -0,0 +1,39 @@ +package main + +import ( + "reflect" + "testing" +) + +func Test_find(t *testing.T) { + type args struct { + target int + candidates []int + } + tests := []struct { + name string + args args + want [][]int + }{ + { + "test1", + args{ + 8, + []int{10, 1, 2, 7, 6, 1, 5}, + }, + [][]int{ + {1, 7}, + {1, 2, 5}, + {2, 6}, + {1, 1, 6}, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := find(tt.args.target, tt.args.candidates); !reflect.DeepEqual(got, tt.want) { + t.Errorf("find() = %v, want %v", got, tt.want) + } + }) + } +}