题解 | #数组分组#

数组分组

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的解法,看了很多递归都不太理解,高赞第一个确实最好理解

全部评论

相关推荐

肖先生~:大一点得到公司面试更能学到点东西
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务