@@ -26,31 +26,25 @@ public function __construct(array $container,$num)
26
26
$ count = count ($ container );
27
27
$ lower = $ key = $ result = 0 ;
28
28
$ high = $ count - 1 ;
29
-
30
29
//计算$count位于斐波那契数列的位置
31
30
while ($ count > ($ this ->produced ($ key ) - 1 )) {
32
31
$ key ++;
33
32
}
34
-
35
33
//将不满的数值补全,补的数值为数组的最后一位
36
34
for ($ j = $ count ; $ j < $ this ->produced ($ key ) - 1 ; $ j ++) {
37
35
$ container [ $ j ] = $ container [ $ count - 1 ];
38
36
}
39
-
40
37
//查找开始
41
38
while ($ lower <= $ high ) {
42
-
43
39
//计算当前分隔的下标
44
40
$ mid = $ lower + $ this ->produced ($ key - 1 ) - 1 ;
45
-
46
41
if ($ num < $ container [ $ mid ]) {
47
- $ high = $ mid - 1 ;
48
- $ key -= 1 ; //斐波那契数列数列下标减一位
42
+ $ high = $ mid - 1 ;
43
+ $ key -= 1 ; //斐波那契数列数列下标减一位
49
44
}else if ($ num > $ container [ $ mid ]) {
50
45
$ lower = $ mid + 1 ;
51
- $ key -= 2 ; //斐波那契数列数列下标减两位
46
+ $ key -= 2 ; //斐波那契数列数列下标减两位
52
47
}
53
-
54
48
if ($ mid <= $ count - 1 ) {
55
49
$ result = $ mid ; break ;
56
50
} else { //这里$mid大于$count-1说明是补全数值,返回$count-1
@@ -66,12 +60,13 @@ public function __construct(array $container,$num)
66
60
* @param $length
67
61
* @return int
68
62
*/
69
- public function produced ($ length ){
63
+ public function produced ($ length )
64
+ {
70
65
if ($ length < 2 ){
71
66
return ($ length == 0 ? 0 : 1 );
72
67
}
73
68
return $ this ->produced ($ length - 1 ) + $ this ->produced ($ length - 2 );
74
69
}
75
70
}
76
71
77
- var_dump ( new FibonacciQuery ([4 , 5 , 7 , 8 , 9 , 10 , 8 ], 8 ) );
72
+ new FibonacciQuery ([4 , 5 , 7 , 8 , 9 , 10 , 8 ], 8 );
0 commit comments