算法inGolang:D&C(分而治之)D&C算法(策略)Divide&Conquer属于递归算法的一种其实它更像是一种思路、策略递归递归Recursion基线条件BaseCase递归条件RecursiveCaseD&C的步骤找到
~/Code/go via 🐹 v1.20.3 via 🅒 base
➜ mcd divide_conquer
Code/go/divide_conquer via 🐹 v1.20.3 via 🅒 base
➜ go mod init divide_conquer
go: creating new go.mod: module divide_conquer
Code/go/divide_conquer via 🐹 v1.20.3 via 🅒 base
➜ c
Code/go/divide_conquer via 🐹 v1.20.3 via 🅒 base
➜
package main
import "fmt"
func main() {
total := sum([]int{1, 3, 5, 7, 9})
fmt.Println("total: ", total)
}
func sum(arr []int) int {
if len(arr) == 0 {
return 0
}
return arr[0] + sum(arr[1:])
}
Code/go/divide_conquer via 🐹 v1.20.3 via 🅒 base
➜ go run .
total: 25
Code/go/divide_conquer via 🐹 v1.20.3 via 🅒 base
➜
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!