Skip to content

Commit 2f271ff

Browse files
committed
feat: the new algorithm 📈
1 parent 2ed8fa8 commit 2f271ff

File tree

6 files changed

+170
-11
lines changed

6 files changed

+170
-11
lines changed

package/Other/BigSmallReplace.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
*
1010
* segmentFault: https://segmentfault.com/q/1010000010627229
1111
*
12-
* @language php
13-
* @author Pu Shaowei
1412
* @param $str
1513
* @return string
1614
*/

package/Other/Fibonacci.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Fibonacci
4+
*
5+
* @author Pu ShaoWei <[email protected]>
6+
* @date 2017/8/25
7+
* @license Mozilla
8+
* -------------------------------------------------------------
9+
* 思路分析:这他妈有个毛的思路,随便学习一下
10+
* -------------------------------------------------------------
11+
* 斐波那契数列(Fibonacci Sequence)又称黄金分割数列
12+
* 指的是这样一个数列:1、1、2、3、5、8、13、21
13+
* 在数学上,斐波纳契数列以如下被以递归的方法定义:F0=0,F1=1,Fn=F(n-1)+F(n-2)(n>=2,n∈N*)。
14+
*
15+
* @param $n
16+
* @return int
17+
*/
18+
19+
// recursion
20+
/*
21+
function Fibonacci($n)
22+
{
23+
if ($n <= 1 ) {
24+
return $n;
25+
}
26+
return Fibonacci($n - 1) + Fibonacci($n - 2);
27+
}
28+
*/
29+
// 55
30+
31+
// not recursion
32+
function Fibonacci($n)
33+
{
34+
if ($n <= 1) {
35+
return $n;
36+
}
37+
for ($fib = [0, 1], $i = 2; $i <= $n; $i++) {
38+
$fib[$i] = $fib[$i - 1] + $fib[$i - 2];
39+
}
40+
return $fib[$n];
41+
}
42+
43+
echo Fibonacci(10);
44+
// 55

package/Other/GetCattle.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,45 @@
33
/**
44
* GetCattle
55
*
6-
* @author Pu ShaoWei <[email protected]>
6+
* @author Pu ShaoWei <[email protected]>
77
* @date 2017/8/24
88
* @license Mozilla
9+
* -------------------------------------------------------------
10+
* 思路分析:见下方注释 
11+
* -------------------------------------------------------------
12+
*
13+
* 牛年求牛:
14+
* 有一母牛,到4岁可生育,每年一头,所生均是一样的母牛
15+
* 15岁绝育,不再能生,
16+
* 20岁死亡,问n年后有多少头牛。
17+
*
18+
* @param $n
19+
* @return int
920
*/
10-
class GetCattle
21+
function getCattle($n)
1122
{
23+
static $num = 1;
24+
for ($i =1; $i<=$n;$i++){
25+
if( $i == 20) break;
26+
if($i >= 4 && $i <15){
27+
if($i % 4 == 0 ){
28+
getCattle($n - $i);
29+
$num++;
30+
}
31+
$num++;
32+
}
33+
}
34+
return $num;
35+
}
36+
echo '牛年共有:'.getCattle(10);
37+
38+
/*
39+
123456789
40+
123456
41+
123
42+
12
43+
9 - 11
44+
45+
---
1246
13-
}
47+
*/

package/Other/MonkeyKing.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @date 2017/8/23
88
* @license Mozilla
99
* -------------------------------------------------------------
10-
* 思路分析:单项循环数据链表 
10+
* 思路分析:约瑟夫环问题
1111
* -------------------------------------------------------------
1212
*有M个monkey ,转成一圈,第一个开始数数,数到第N个出圈,下一个再从1开始数,再数到第N个出圈,直到圈里只剩最后一个就是大王
1313
*/
@@ -64,3 +64,12 @@ public static function whoIsKing($count, $num)
6464
// 共10个猴子每3个出圈
6565
var_dump(MonkeyKing::whoIsKing(10, 3));
6666

67+
function whoIsKing($n,$m){
68+
$r = 0;
69+
for ($i=2; $i <= $n ; $i++) {
70+
$r = ($r + $m) % $i;
71+
}
72+
return $r+1;
73+
}
74+
75+
var_dump(whoIsKing(10,3));

package/Query/BinaryTree.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
<?php
2+
23
/**
3-
* ${NAME}
4+
* BinaryTree
45
*
5-
* @author Pu ShaoWei <[email protected]>
6+
* @author Pu ShaoWei <[email protected]>
67
* @date 2017/8/25
78
* @license Mozilla
9+
* -------------------------------------------------------------
10+
* 思路分析:
11+
* -------------------------------------------------------------
12+
*
813
*/

package/Query/FibonacciQuery.php

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,77 @@
11
<?php
22
/**
3-
* ${NAME}
3+
* FibonacciQuery
44
*
5-
* @author Pu ShaoWei <[email protected]>
6-
* @date 2017/8/25
5+
* @author Pu ShaoWei <[email protected]>
6+
* @date 2017/8/23
77
* @license Mozilla
8+
* -------------------------------------------------------------
9+
* 思路分析:斐波那契查找 利用黄金分割原理
10+
* -------------------------------------------------------------
11+
*
12+
* $num == $container[$mid],直接返回
13+
* $num < $container[$mid],新范围是第 $low 个到 $mid-1 个,此时范围个数为 produced($key-1)-1 个
14+
* $num > $container[$mid],新范围是第 $mid+1 个到 $high 个,此时范围个数为 produced($key-2)-1 个
815
*/
16+
class FibonacciQuery{
17+
18+
/**
19+
* FibonacciQuery constructor.
20+
*
21+
* @param array $container
22+
* @param $num
23+
*/
24+
public function __construct(array $container,$num)
25+
{
26+
$count = count($container);
27+
$lower = $key = $result = 0;
28+
$high = $count - 1;
29+
30+
//计算$count位于斐波那契数列的位置
31+
while ($count > ($this->produced($key) - 1)) {
32+
$key++;
33+
}
34+
35+
//将不满的数值补全,补的数值为数组的最后一位
36+
for ($j = $count; $j < $this->produced($key) - 1; $j++) {
37+
$container[ $j ] = $container[ $count - 1 ];
38+
}
39+
40+
//查找开始
41+
while ($lower <= $high) {
42+
43+
//计算当前分隔的下标
44+
$mid = $lower + $this->produced($key - 1) - 1;
45+
46+
if ($num < $container[ $mid ]) {
47+
$high = $mid - 1;
48+
$key -= 1; //斐波那契数列数列下标减一位
49+
}else if ($num > $container[ $mid ]) {
50+
$lower = $mid + 1;
51+
$key -= 2; //斐波那契数列数列下标减两位
52+
}
53+
54+
if ($mid <= $count - 1) {
55+
$result = $mid; break;
56+
} else { //这里$mid大于$count-1说明是补全数值,返回$count-1
57+
$result = $count - 1; break;
58+
}
59+
}
60+
var_dump( $result );
61+
}
62+
63+
/**
64+
* 创建一个生产斐波那契数列
65+
*
66+
* @param $length
67+
* @return int
68+
*/
69+
public function produced($length){
70+
if($length < 2){
71+
return ($length == 0 ? 0 : 1);
72+
}
73+
return $this->produced($length - 1) + $this->produced($length - 2);
74+
}
75+
}
76+
77+
var_dump(new FibonacciQuery([4, 5, 7, 8, 9, 10, 8], 8));

0 commit comments

Comments
 (0)