{"action":"opened","issue":{"url":"https://api.github.com/repos/yankewei/blog/issues/8","repository_url":"https://api.github.com/repos/yankewei/blog","labels_url":"https://api.github.com/repos/yankewei/blog/issues/8/labels{/name}","comments_url":"https://api.github.com/repos/yankewei/blog/issues/8/comments","events_url":"https://api.github.com/repos/yankewei/blog/issues/8/events","html_url":"https://github.com/yankewei/blog/issues/8","id":335830259,"node_id":"MDU6SXNzdWUzMzU4MzAyNTk=","number":8,"title":"【PHP】匿名函数递归","user":{"login":"yankewei","id":19988359,"node_id":"MDQ6VXNlcjE5OTg4MzU5","avatar_url":"https://avatars2.githubusercontent.com/u/19988359?v=4","gravatar_id":"","url":"https://api.github.com/users/yankewei","html_url":"https://github.com/yankewei","followers_url":"https://api.github.com/users/yankewei/followers","following_url":"https://api.github.com/users/yankewei/following{/other_user}","gists_url":"https://api.github.com/users/yankewei/gists{/gist_id}","starred_url":"https://api.github.com/users/yankewei/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yankewei/subscriptions","organizations_url":"https://api.github.com/users/yankewei/orgs","repos_url":"https://api.github.com/users/yankewei/repos","events_url":"https://api.github.com/users/yankewei/events{/privacy}","received_events_url":"https://api.github.com/users/yankewei/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-06-26T13:52:28Z","updated_at":"2018-06-26T13:52:28Z","closed_at":null,"author_association":"OWNER","body":"### 普通递归\r\n普通函数的递归，大家应该都清楚，并且也都用过，比如说斐波那契数列，快速排序等等。\r\n```php\r\n#递归实现斐波那契额数列\r\nfunction fibonacci(int $in) : int\r\n{\r\n  if ($n <= 0) {\r\n    return $n;\r\n  } elseif ($n === 1) {\r\n    return 1;\r\n  } else {\r\n    return fibonacci($n - 2) + fibonacci($n - 1);\r\n  }\r\n}\r\n```\r\n```php\r\n#快速排序\r\nfunction QuickSort(array $arr) : array\r\n{\r\n  $length = count($arr);\r\n  if ($length <= 1) {\r\n    return $arr;\r\n  }\r\n  $mid = $arr[0];\r\n  $littleArr = [];\r\n  $bigArr = [];\r\n  for ($i = 1; $i < $length; $i++) {\r\n    if ($arr[$i] <= $mid) {\r\n      $littleArr[] = $arr[$i];\r\n    }\r\n    if ($arr[$i] > $mid) {\r\n      $bigArr[] = $arr[$i];\r\n    }\r\n  }\r\n  $littleArr = QuickSort($littleArr);\r\n  $littleArr[] = $mid;\r\n  $bigArr = QuickSort($bigArr);\r\n  return array_merge($littleArr, $bigArr);\r\n}\r\n```\r\n### 匿名函数递归\r\n最近在用php刷一些算法方面的题，就看到一个实现BFS算法的方式, 题目详见[LeetCode](https://leetcode.com/problems/binary-tree-level-order-traversal-ii/description/)\r\n```php\r\nclass TreeNode\r\n{\r\n  public $val;\r\n  public $left;\r\n  public $right;\r\n\r\n  public function __construct($val)\r\n  {\r\n    $this->val = $val;\r\n  }\r\n}\r\n\r\n$twenty = new TreeNode(20);\r\n$twenty->left = new TreeNode(15);\r\n$twenty->right = new TreeNode(7);\r\n$one = new TreeNode(3);\r\n$one->left = new TreeNode(9);\r\n$one->right = $twenty;\r\n\r\nfunction levelOrderBottom(TreeNode $root, int $level)\r\n{\r\n  $arr = [];\r\n  $dfs = function ($root, $level) use (&$arr, &$dfs){\r\n    if (is_null($root)) {\r\n      return;\r\n    }\r\n    $dfs($root->left, $level + 1);\r\n    $dfs($root->right, $level + 1);\r\n    $arr[$level][] = $root->val;\r\n  };\r\n  $dfs($root, 0);\r\n  return $arr;\r\n}\r\nvar_dump(levelOrderBottom($one, 0));\r\n```\r\n在匿名函数使用`use`关键字可以使用函数外部的变量，同样我们也可以使用这个匿名函数。整个过程就是这样了。"}}
{"actor":{"display_login":"yankewei"}}