-
Notifications
You must be signed in to change notification settings - Fork 2
/
programers43165.go
41 lines (36 loc) · 940 Bytes
/
programers43165.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import "fmt"
func main() {
fmt.Println(solution([]int{1, 1, 1, 1, 1}, 3))
}
func solution(numbers []int, target int) int {
answer := 0
nextCalculator(numbers, "+", target, 0, &answer)
nextCalculator(numbers, "-", target, 0, &answer)
return answer
}
func nextCalculator(partialNumber []int, pm string, target int, valueNow int, ans* int) {
if pm == "+" {
value := valueNow
value += partialNumber[0]
if len(partialNumber) == 1 {
if value == target {
*ans = *ans + 1
}
} else {
nextCalculator(partialNumber[1:], "+", target, value, ans)
nextCalculator(partialNumber[1:], "-", target, value, ans)
}
} else if pm == "-" {
value := valueNow
value -= partialNumber[0]
if len(partialNumber) == 1 {
if value == target {
*ans = *ans + 1
}
} else {
nextCalculator(partialNumber[1:], "+", target, value, ans)
nextCalculator(partialNumber[1:], "-", target, value, ans)
}
}
}