From 2fed35ce7073f81ab54333e0cf644c3cce86b3c5 Mon Sep 17 00:00:00 2001 From: YDZ Date: Sun, 25 Aug 2019 22:09:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20problem=20509?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../509. Fibonacci Number.go | 110 ++++++++++++++++++ .../509. Fibonacci Number_test.go | 53 +++++++++ Algorithms/0509. Fibonacci Number/README.md | 57 +++++++++ 3 files changed, 220 insertions(+) create mode 100644 Algorithms/0509. Fibonacci Number/509. Fibonacci Number.go create mode 100644 Algorithms/0509. Fibonacci Number/509. Fibonacci Number_test.go create mode 100755 Algorithms/0509. Fibonacci Number/README.md diff --git a/Algorithms/0509. Fibonacci Number/509. Fibonacci Number.go b/Algorithms/0509. Fibonacci Number/509. Fibonacci Number.go new file mode 100644 index 00000000..fcb0d3a7 --- /dev/null +++ b/Algorithms/0509. Fibonacci Number/509. Fibonacci Number.go @@ -0,0 +1,110 @@ +package leetcode + +import "math" + +// 解法一 递归法 时间复杂度 O(2^n),空间复杂度 O(n) +func fib(N int) int { + if N <= 1 { + return N + } + return fib(N-1) + fib(N-2) +} + +// 解法二 自底向上的记忆化搜索 时间复杂度 O(n),空间复杂度 O(n) +func fib1(N int) int { + if N <= 1 { + return N + } + cache := map[int]int{0: 0, 1: 1} + for i := 2; i <= N; i++ { + cache[i] = cache[i-1] + cache[i-2] + } + return cache[N] +} + +// 解法三 自顶向下的记忆化搜索 时间复杂度 O(n),空间复杂度 O(n) +func fib2(N int) int { + if N <= 1 { + return N + } + return memoize(N, map[int]int{0: 0, 1: 1}) +} + +func memoize(N int, cache map[int]int) int { + if _, ok := cache[N]; ok { + return cache[N] + } + cache[N] = memoize(N-1, cache) + memoize(N-2, cache) + return memoize(N, cache) +} + +// 解法四 优化版的 dp,节约内存空间 时间复杂度 O(n),空间复杂度 O(1) +func fib3(N int) int { + if N <= 1 { + return N + } + if N == 2 { + return 1 + } + current, prev1, prev2 := 0, 1, 1 + for i := 3; i <= N; i++ { + current = prev1 + prev2 + prev2 = prev1 + prev1 = current + } + return current +} + +// 解法五 矩阵快速幂 时间复杂度 O(log n),空间复杂度 O(log n) +// | 1 1 | ^ n = | F(n+1) F(n) | +// | 1 0 | | F(n) F(n-1) | +func fib4(N int) int { + if N <= 1 { + return N + } + var A = [2][2]int{ + {1, 1}, + {1, 0}, + } + A = matrixPower(A, N-1) + return A[0][0] +} + +func matrixPower(A [2][2]int, N int) [2][2]int { + if N <= 1 { + return A + } + A = matrixPower(A, N/2) + A = multiply(A, A) + + var B = [2][2]int{ + {1, 1}, + {1, 0}, + } + if N%2 != 0 { + A = multiply(A, B) + } + + return A +} + +func multiply(A [2][2]int, B [2][2]int) [2][2]int { + x := A[0][0]*B[0][0] + A[0][1]*B[1][0] + y := A[0][0]*B[0][1] + A[0][1]*B[1][1] + z := A[1][0]*B[0][0] + A[1][1]*B[1][0] + w := A[1][0]*B[0][1] + A[1][1]*B[1][1] + A[0][0] = x + A[0][1] = y + A[1][0] = z + A[1][1] = w + return A +} + +// 解法六 公式法 f(n)=(1/√5)*{[(1+√5)/2]^n -[(1-√5)/2]^n},用 时间复杂度在 O(log n) 和 O(n) 之间,空间复杂度 O(1) +// 经过实际测试,会发现 pow() 系统函数比快速幂慢,说明 pow() 比 O(log n) 慢 +// 斐波那契数列是一个自然数的数列,通项公式却是用无理数来表达的。而且当 n 趋向于无穷大时,前一项与后一项的比值越来越逼近黄金分割 0.618(或者说后一项与前一项的比值小数部分越来越逼近 0.618)。 +// 斐波那契数列用计算机计算的时候可以直接用四舍五入函数 Round 来计算。 +func fib5(N int) int { + var goldenRatio float64 = float64((1 + math.Sqrt(5)) / 2) + return int(math.Round(math.Pow(goldenRatio, float64(N)) / math.Sqrt(5))) +} diff --git a/Algorithms/0509. Fibonacci Number/509. Fibonacci Number_test.go b/Algorithms/0509. Fibonacci Number/509. Fibonacci Number_test.go new file mode 100644 index 00000000..cc7bf04c --- /dev/null +++ b/Algorithms/0509. Fibonacci Number/509. Fibonacci Number_test.go @@ -0,0 +1,53 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question509 struct { + para509 + ans509 +} + +// para 是参数 +// one 代表第一个参数 +type para509 struct { + one int +} + +// ans 是答案 +// one 代表第一个答案 +type ans509 struct { + one int +} + +func Test_Problem509(t *testing.T) { + + qs := []question509{ + + question509{ + para509{1}, + ans509{1}, + }, + + question509{ + para509{2}, + ans509{1}, + }, + + question509{ + para509{3}, + ans509{2}, + }, + // 如需多个测试,可以复制上方元素。 + } + + fmt.Printf("------------------------Leetcode Problem 509------------------------\n") + + for _, q := range qs { + _, p := q.ans509, q.para509 + fmt.Printf("【input】:%v 【output】:%v\n", p, fib(p.one)) + } + fmt.Printf("\n\n\n") +} diff --git a/Algorithms/0509. Fibonacci Number/README.md b/Algorithms/0509. Fibonacci Number/README.md new file mode 100755 index 00000000..ff0ca5da --- /dev/null +++ b/Algorithms/0509. Fibonacci Number/README.md @@ -0,0 +1,57 @@ +# 509. Fibonacci Number + +Created Time: Aug 24, 2019 2:52 PM +Difficulty: Easy +Link: https://leetcode.com/problems/fibonacci-number/ +Tags: Array,Dynamic Programming,Math,Recursive + +## 题目: + +The **Fibonacci numbers**, commonly denoted `F(n)` form a sequence, called the **Fibonacci sequence**, such that each number is the sum of the two preceding ones, starting from `0` and `1`. That is, + + F(0) = 0,   F(1) = 1 + F(N) = F(N - 1) + F(N - 2), for N > 1. + +Given `N`, calculate `F(N)`. + +**Example 1:** + + Input: 2 + Output: 1 + Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1. + +**Example 2:** + + Input: 3 + Output: 2 + Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2. + +**Example 3:** + + Input: 4 + Output: 3 + Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3. + +**Note:** + +0 ≤ `N` ≤ 30. + + +## 题目大意 + +斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是: + +``` +F(0) = 0, F(1) = 1 +F(N) = F(N - 1) + F(N - 2), 其中 N > 1. +``` + +给定 N,计算 F(N)。 + +提示:0 ≤ N ≤ 30 + +## 解题思路 + + +- 求斐波那契数列 +- 这一题解法很多,大的分类是四种,递归,记忆化搜索(dp),矩阵快速幂,通项公式。其中记忆化搜索可以写 3 种方法,自底向上的,自顶向下的,优化空间复杂度版的。通项公式方法实质是求 a^b 这个还可以用快速幂优化时间复杂度到 O(log n) 。