Leetcode Note: Go - Fibonacci Number

Fibonacci Number - LeetCode
https://leetcode.com/problems/fibonacci-number/submissions/

  • Go 言語で取り組んだメモ

所感

  • フィボナッチ数を求める

回答

Using dynamic programming(Memoization) in Golang - LeetCode Discuss
https://leetcode.com/problems/fibonacci-number/discuss/1214542/Using-dynamic-programming(Memoization)-in-Golang

func fib(n int) int {
    var m = make(map[int]int)

    if n == 0 {
        return 0
    }

    if _, ok := m[n]; ok {
		return m[n]
	}

	if n <= 2 {
		return 1
	}

	m[n] = fib((n - 1)) + fib((n - 2))
	return m[n]
}
  • 重複チェックしつつ再帰