Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
fangzesheng committed Sep 10, 2020
1 parent 80d8955 commit cac0825
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
composer.lock
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/algorithm.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,43 @@
# algorithm

Algorithm for PHP
----
* 运行最底要求 PHP 版本 5.4 , 建议在 PHP7 上运行以获取最佳性能;
* 算法从互联网上整理所得,如有侵权请联系我删除;
* Algorithm 可以帮助大多数phper算法不怎么好的童鞋,欢迎 fork 或 star 此项目。

功能描述
----
* 实现某一指定算法
* 查看其解题思路和code


安装使用
----
1 通过 Composer 来管理安装
```shell
# 首次安装 线上版本(稳定)
composer require fangzesheng/algorithm

# 更新 algorithm
composer update fangzesheng/algorithm
```

2 实例指定算法
```php
try {
// twoNumberSum为算法题目,[[1,3,4,56,34,5,2,6],8]为该算法所需要参数,该算法求两个数和所需两个参数,分别为[1,3,4,56,34,5,2,6]和8
$upload = new Algorithm('twoNumberSum',[[1,3,4,56,34,5,2,6],8]);
print_r($upload->achieve());
} catch (Exception $e) {
// 出错啦,处理下吧
echo $e->getMessage() . PHP_EOL;
}
```


* 更多算法详情请阅读代码





18 changes: 18 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "fangzesheng/algorithm",
"authors": [
{
"name": "fangzesheng",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"require": {
"php":">=5.6"
},
"autoload": {
"psr-4": {
"Algorithm\\": "src/"
}
}
}
25 changes: 25 additions & 0 deletions src/Algorithm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace Algorithm;
class Algorithm
{
private $Algorithm;
private $data;
public function __construct($name, $data)
{
$this->data = $data;
$application = "\\Algorithm\\Subject\\{$name}";
$this->Algorithm = new $application();
}
public function achieve()
{
return $this->Algorithm->achieve(...$this->data);
}
public function thinking()
{
return $this->Algorithm->thinking();
}
public function code()
{
return $this->Algorithm->code();
}
}
33 changes: 33 additions & 0 deletions src/Subject/twoNumberSum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace Algorithm\Subject;
class twoNumberSum
{
public function achieve($nums, $target)
{
foreach ($nums as $k=> $v){
if(isset($result[$target - $v]) && $nums[$result[$target - $v]] + $v === $target) {
return [$result[$target - $v],$k];
}
$result[$v] = $k;
}
return $result ?? [];
}
public function thinking()
{
return <<<EOF
方法一:暴力法
暴力法很简单,遍历每个元素 xx,并查找是否存在一个值与 target - xtarget−x 相等的目标元素。
方法二:两遍哈希表
为了对运行时间复杂度进行优化,我们需要一种更有效的方法来检查数组中是否存在目标元素。如果存在,我们需要找出它的索引。保持数组中的每个元素与其索引相互对应的最好方法是什么?哈希表。
通过以空间换取速度的方式,我们可以将查找时间从 O(n)O(n) 降低到 O(1)O(1)。哈希表正是为此目的而构建的,它支持以 近似 恒定的时间进行快速查找。我用“近似”来描述,是因为一旦出现冲突,查找用时可能会退化到 O(n)O(n)。但只要你仔细地挑选哈希函数,在哈希表中进行查找的用时应当被摊销为 O(1)O(1)。
一个简单的实现使用了两次迭代。在第一次迭代中,我们将每个元素的值和它的索引添加到表中。然后,在第二次迭代中,我们将检查每个元素所对应的目标元素(target - nums[i]target−nums[i])是否存在于表中。注意,该目标元素不能是 nums[i]nums[i] 本身!
方法三:一遍哈希表
事实证明,我们可以一次完成。在进行迭代并将元素插入到表中的同时,我们还会回过头来检查表中是否已经存在当前元素所对应的目标元素。如果它存在,那我们已经找到了对应解,并立即将其返回。
EOF;
}
public function code(){
show_source("__FILE__",true);
}
}
6 changes: 6 additions & 0 deletions test/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
require_once '../vendor/autoload.php';

use Algorithm\Algorithm;
$upload = new Algorithm('twoNumberSum',[[1,3,4,56,34,5,2,6],8]);
print_r($upload->achieve());

0 comments on commit cac0825

Please sign in to comment.