algo/go/10_recursion/Fibonacci.go

36 lines
533 B
Go
Raw Normal View History

2019-05-15 22:36:32 +08:00
package Recursion
import "fmt"
// 递归实现斐波那契数列
type Fibs struct {
val map[int]int // 使用字典存储结果
}
func NewFibs(n int) *Fibs {
return &Fibs{
make(map[int]int, n),
}
}
func (fib *Fibs)Fibonacci(n int) int {
if fib.val[n] != 0{
return fib.val[n]
}
if n <= 1 {
fib.val[1] = 1
return 1
}else if n ==2{
fib.val[2] = 1
return 1
} else {
res := fib.Fibonacci(n-1) + fib.Fibonacci(n-2)
fib.val[n] = res
return res
}
}
func (fib *Fibs)Print(n int) {
fmt.Println(fib.val[n])
}