-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfn.php
More file actions
35 lines (29 loc) · 679 Bytes
/
fn.php
File metadata and controls
35 lines (29 loc) · 679 Bytes
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
<?php
class Solution
{
public $ans = [];
public function fib($n)
{
// if ($n <= 1) {
// return $n;
// } else {
// if (!empty($this->ans[$n])) {
// return $this->ans[$n];
// }
// return $this->ans[] = $this->fib($n - 2) + $this->fib($n - 1);
// }
$a = [];
$a[0] = 0;
$a[1] = 1;
for ($i = 2; $i < $n; $i++) {
if ($i <= 1) {
$a[$i];
} else {
$a[$i] = $a[$i - 1] + $a[$i - 2];
}
}
return $a[count($a) - 1];
}
}
$obj = new Solution();
print_r($obj->fib(5));