Skip to content

Commit 1cbd8fa

Browse files
committed
fix: modify bug ⬆️
1 parent 0eddd4f commit 1cbd8fa

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

package/Other/Fibonacci.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
* @date 2017/8/25
77
* @license Mozilla
88
* -------------------------------------------------------------
9-
* 思路分析:这他妈有个毛的思路,随便学习一下
9+
* 思路分析:
1010
* -------------------------------------------------------------
11-
* 斐波那契数列(Fibonacci Sequence)又称黄金分割数列
11+
* 斐波那契数列(Fibonacci Sequence)又称黄金分割数列 兔子数列
1212
* 指的是这样一个数列:1、1、2、3、5、8、13、21
1313
* 在数学上,斐波纳契数列以如下被以递归的方法定义:F0=0,F1=1,Fn=F(n-1)+F(n-2)(n>=2,n∈N*)。
1414
*

package/Query/FibonacciQuery.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,25 @@ public function __construct(array $container,$num)
2626
$count = count($container);
2727
$lower = $key = $result = 0;
2828
$high = $count - 1;
29-
3029
//计算$count位于斐波那契数列的位置
3130
while ($count > ($this->produced($key) - 1)) {
3231
$key++;
3332
}
34-
3533
//将不满的数值补全,补的数值为数组的最后一位
3634
for ($j = $count; $j < $this->produced($key) - 1; $j++) {
3735
$container[ $j ] = $container[ $count - 1 ];
3836
}
39-
4037
//查找开始
4138
while ($lower <= $high) {
42-
4339
//计算当前分隔的下标
4440
$mid = $lower + $this->produced($key - 1) - 1;
45-
4641
if ($num < $container[ $mid ]) {
47-
$high = $mid - 1;
48-
$key -= 1; //斐波那契数列数列下标减一位
42+
$high = $mid - 1;
43+
$key -= 1; //斐波那契数列数列下标减一位
4944
}else if ($num > $container[ $mid ]) {
5045
$lower = $mid + 1;
51-
$key -= 2; //斐波那契数列数列下标减两位
46+
$key -= 2; //斐波那契数列数列下标减两位
5247
}
53-
5448
if ($mid <= $count - 1) {
5549
$result = $mid; break;
5650
} else { //这里$mid大于$count-1说明是补全数值,返回$count-1
@@ -66,12 +60,13 @@ public function __construct(array $container,$num)
6660
* @param $length
6761
* @return int
6862
*/
69-
public function produced($length){
63+
public function produced($length)
64+
{
7065
if($length < 2){
7166
return ($length == 0 ? 0 : 1);
7267
}
7368
return $this->produced($length - 1) + $this->produced($length - 2);
7469
}
7570
}
7671

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

Comments
 (0)