Skip to content

Commit

Permalink
两数之和
Browse files Browse the repository at this point in the history
  • Loading branch information
fangzesheng committed Sep 10, 2020
1 parent cac0825 commit 879deef
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
16 changes: 12 additions & 4 deletions src/Algorithm.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,24 @@ public function __construct($name, $data)
$application = "\\Algorithm\\Subject\\{$name}";
$this->Algorithm = new $application();
}
/**
* 算法
*
* @access public
* @return mixed
*/
public function achieve()
{
return $this->Algorithm->achieve(...$this->data);
}
/**
* 思路
*
* @access public
* @return array
*/
public function thinking()
{
return $this->Algorithm->thinking();
}
public function code()
{
return $this->Algorithm->code();
}
}
25 changes: 22 additions & 3 deletions src/Subject/twoNumberSum.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
<?php
/**
*两数之和
*
* @author fzs
* @version 1.0 版本号
*/
namespace Algorithm\Subject;

class twoNumberSum
{
/**
* 两数之和算法
*
* @access public
* @param array $nums 整数数组
* @param int $target 目标值
* @return array
*/
public function achieve($nums, $target)
{
foreach ($nums as $k=> $v){
Expand All @@ -12,6 +27,13 @@ public function achieve($nums, $target)
}
return $result ?? [];
}
/**
* thinking
* 两数之和算法思路
*
* @access public
* @return string
*/
public function thinking()
{
return <<<EOF
Expand All @@ -27,7 +49,4 @@ public function thinking()
事实证明,我们可以一次完成。在进行迭代并将元素插入到表中的同时,我们还会回过头来检查表中是否已经存在当前元素所对应的目标元素。如果它存在,那我们已经找到了对应解,并立即将其返回。
EOF;
}
public function code(){
show_source("__FILE__",true);
}
}

0 comments on commit 879deef

Please sign in to comment.