题解 | #数组分组#
数组分组
https://www.nowcoder.com/practice/9af744a3517440508dbeb297020aca86
package main
import "fmt"
func main() {
var count int
fmt.Scan(&count)
var otherSlice []int
var inputNum int
exceptSum := 0
for i := 0; i < count; i++ {
fmt.Scan(&inputNum)
if inputNum%5 == 0 {
exceptSum += inputNum
} else if inputNum%3 == 0 {
exceptSum -= inputNum
} else {
otherSlice = append(otherSlice, inputNum)
}
}
fmt.Println(dfs(otherSlice, exceptSum))
}
func dfs(otherSlice []int, exceptSum int) bool {
if len(otherSlice) == 0 {
if exceptSum == 0 {
return true
}
return false
}
return dfs(otherSlice[1:], exceptSum+otherSlice[0]) || dfs(otherSlice[1:], exceptSum-otherSlice[0])
}
提供第一个golang的解法,看了很多递归都不太理解,高赞第一个确实最好理解
