-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest.html
More file actions
59 lines (56 loc) · 1.49 KB
/
test.html
File metadata and controls
59 lines (56 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<script>
// 普通递归
// function fibo(n) {
// if (n === 0) return 0
// if (n === 1) return 1
// return fibo(n - 1) + fibo(n - 2)
// }
// 使用数据存储之前计算结果,以复用
// function fibo(n) {
// if (n === 0) return 0
// if (n === 1) return 1
// let arr = [0, 1]
// for (let i = 2; i <= n; i++) {
// arr[i] = arr[i - 1] + arr[i - 2]
// }
// return arr[n]
// }
// 减少空间,用变量替换数组
// function fibo(n) {
// if (n === 0) return 0
// if (n === 1) return 1
// let res = 0
// let a = 0
// let b = 1
// for (let i = 2; i <= n; i++) {
// res = a + b
// a = b
// b = res
// }
// return res
// }
// 还可以更简洁,再节省一个变量
function fibo(n) {
if (n === 0) return 0
if (n === 1) return 1
let a = 0
let b = 1
for (let i = 2; i <= n; i++) {
b = a + b
a = b - a
}
return b
}
console.log(fibo(20));
</script>
</body>
</html>