-
Notifications
You must be signed in to change notification settings - Fork 15.1k
add php code #1826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
piaohan
wants to merge
3
commits into
krahets:main
Choose a base branch
from
piaohan:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add php code #1826
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| <?php | ||
| /** | ||
| * File: iteration.php | ||
| * Created Time: 2025-11-06 | ||
| * Author: Leo Mu (whatissrc@gmail.com) | ||
| */ | ||
|
|
||
| namespace chapter_computational_complexity; | ||
|
|
||
| class iteration | ||
| { | ||
| /* for 循环 */ | ||
| public static function forLoop($n): int | ||
| { | ||
| $res = 0; | ||
| // 循环求和 1, 2, ..., n-1, n | ||
| for ($i = 1; $i <= $n; $i++) { | ||
| $res += $i; | ||
| } | ||
| return $res; | ||
| } | ||
|
|
||
| /* while 循环 */ | ||
| public static function whileLoop($n): int | ||
| { | ||
| $res = 0; | ||
| $i = 1; // 初始化条件变量 | ||
| // 循环求和 1, 2, ..., n-1, n | ||
| while ($i <= $n) { | ||
| $res += $i; | ||
| $i++; // 更新条件变量 | ||
| } | ||
| return $res; | ||
| } | ||
|
|
||
| /* while 循环(两次更新) */ | ||
| public static function whileLoopII($n): int | ||
| { | ||
| $res = 0; | ||
| $i = 1; // 初始化条件变量 | ||
| // 循环求和 1, 4, 10, ... | ||
| while ($i <= $n) { | ||
| $res += $i; | ||
| // 更新条件变量 | ||
| $i++; | ||
| $i *= 2; | ||
| } | ||
| return $res; | ||
| } | ||
|
|
||
| /* 双层 for 循环 */ | ||
| public static function nestedForLoop($n): string | ||
| { | ||
| $res = ''; | ||
| // 循环 i = 1, 2, ..., n-1, n | ||
| for ($i = 1; $i <= $n; $i++) { | ||
| // 循环 j = 1, 2, ..., n-1, n | ||
| for ($j = 1; $j <= $n; $j++) { | ||
| $res .= "($i, $j), "; | ||
| } | ||
| } | ||
| return $res; | ||
| } | ||
| } | ||
|
|
||
| /* Driver Code */ | ||
| $n = 5; | ||
|
|
||
| $res = Iteration::forLoop($n); | ||
| echo "\nfor 循环的求和结果 res = " . $res . "\n"; | ||
|
|
||
| $res = Iteration::whileLoop($n); | ||
| echo "\nwhile 循环的求和结果 res = " . $res . "\n"; | ||
|
|
||
| $res = Iteration::whileLoopII($n); | ||
| echo "\nwhile 循环(两次更新)求和结果 res = " . $res . "\n"; | ||
|
|
||
| $resStr = Iteration::nestedForLoop($n); | ||
| echo "\n双层 for 循环的遍历结果 " . $resStr . "\n"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| <?php | ||
| /** | ||
| * File: recursion.php | ||
| * Created Time: 2025-11-06 | ||
| * Author: Leo Mu (whatissrc@gmail.com) | ||
| */ | ||
|
|
||
| namespace chapter_computational_complexity; | ||
|
|
||
| class recursion | ||
| { | ||
| /* 递归 */ | ||
| public static function recur(int $n): int | ||
| { | ||
| // 终止条件 | ||
| if ($n == 1) { | ||
| return 1; | ||
| } | ||
| // 递:递归调用 | ||
| $res = self::recur($n - 1); | ||
| // 归:返回结果 | ||
| return $n + $res; | ||
| } | ||
|
|
||
| /* 使用迭代模拟递归 */ | ||
| public static function forLoopRecur(int $n): int | ||
| { | ||
| // 使用一个显式的数组来模拟系统调用栈 | ||
| $stack = []; | ||
| $res = 0; | ||
| // 递:递归调用 | ||
| for ($i = $n; $i > 0; $i--) { | ||
| // 通过"入栈操作"模拟"递" | ||
| array_push($stack, $i); | ||
| } | ||
| // 归:返回结果 | ||
| while (!empty($stack)) { | ||
| // 通过"出栈操作"模拟"归" | ||
| $res += array_pop($stack); | ||
| } | ||
| // res = 1+2+3+...+n | ||
| return $res; | ||
| } | ||
|
|
||
| /* 尾递归 */ | ||
| public static function tailRecur(int $n, int $res): int | ||
| { | ||
| // 终止条件 | ||
| if ($n == 0) { | ||
| return $res; | ||
| } | ||
| // 尾递归调用 | ||
| return self::tailRecur($n - 1, $res + $n); | ||
| } | ||
|
|
||
| /* 斐波那契数列:递归 */ | ||
| public static function fib(int $n): int | ||
| { | ||
| // 终止条件 f(1) = 0, f(2) = 1 | ||
| if ($n == 1 || $n == 2) { | ||
| return $n - 1; | ||
| } | ||
| // 递归调用 f(n) = f(n-1) + f(n-2) | ||
| $res = self::fib($n - 1) + self::fib($n - 2); | ||
| // 返回结果 f(n) | ||
| return $res; | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
| /* Driver Code */ | ||
| $n = 5; | ||
| $res = 0; | ||
| Recursion::recur($n); | ||
| echo "\n递归函数的求和结果 res = {$res}\n"; | ||
|
|
||
| $res = Recursion::forLoopRecur($n); | ||
| echo "\n使用迭代模拟递归求和结果 res = {$res}\n"; | ||
|
|
||
| $res = Recursion::tailRecur($n, 0); | ||
| echo "\n尾递归函数的求和结果 res = {$res}\n"; | ||
|
|
||
| $res = Recursion::fib($n); | ||
| echo "\n斐波那契数列的第 {$n} 项为 {$res}\n"; |
118 changes: 118 additions & 0 deletions
118
codes/php/chapter_computational_complexity/space_complexity.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| <?php | ||
| /** | ||
| * File: space_complexity.php | ||
| * Created Time: 2025-11-06 | ||
| * Author: Leo Mu (whatissrc@gmail.com) | ||
| */ | ||
|
|
||
| namespace chapter_computational_complexity; | ||
| class space_complexity | ||
| { | ||
| /* 函数 */ | ||
| public static function functionMethod(): int | ||
| { | ||
| // 执行某些操作 | ||
| return 0; | ||
| } | ||
|
|
||
| /* 常数阶 */ | ||
| public static function constant(int $n): void | ||
| { | ||
| // 常量、变量、对象占用 O(1) 空间 | ||
| $a = 0; | ||
| $b = 0; | ||
| $nums = array_fill(0, 10000, 0); | ||
| // 循环中的变量占用 O(1) 空间 | ||
| for ($i = 0; $i < $n; $i++) { | ||
| $c = 0; | ||
| } | ||
| // 循环中的函数占用 O(1) 空间 | ||
| for ($i = 0; $i < $n; $i++) { | ||
| self::functionMethod(); | ||
| } | ||
| } | ||
|
|
||
| /* 线性阶 */ | ||
| public static function linear(int $n): void | ||
| { | ||
| // 长度为 n 的数组占用 O(n) 空间 | ||
| $nums = array_fill(0, $n, 0); | ||
| // 长度为 n 的列表占用 O(n) 空间 | ||
| $nodes = []; | ||
| for ($i = 0; $i < $n; $i++) { | ||
| $nodes[] = ['val' => $i]; | ||
| } | ||
| // 长度为 n 的哈希表占用 O(n) 空间 | ||
| $map = []; | ||
| for ($i = 0; $i < $n; $i++) { | ||
| $map[$i] = strval($i); | ||
| } | ||
| } | ||
|
|
||
| /* 线性阶(递归实现)*/ | ||
| public static function linearRecur(int $n): void | ||
| { | ||
| echo "递归 n = " . $n . "\n"; | ||
| if ($n == 1) { | ||
| return; | ||
| } | ||
| self::linearRecur($n - 1); | ||
| } | ||
|
|
||
| /* 平方阶 */ | ||
| public static function quadratic(int $n): void | ||
| { | ||
| // 矩阵占用 O(n^2) 空间 | ||
| $numMatrix = []; | ||
| for ($i = 0; $i < $n; $i++) { | ||
| $numMatrix[$i] = array_fill(0, $n, 0); | ||
| } | ||
| // 二维列表占用 O(n^2) 空间 | ||
| $numList = []; | ||
| for ($i = 0; $i < $n; $i++) { | ||
| $tmp = []; | ||
| for ($j = 0; $j < $n; $j++) { | ||
| $tmp[] = 0; | ||
| } | ||
| $numList[] = $tmp; | ||
| } | ||
| } | ||
|
|
||
| /* 平方阶(递归实现) */ | ||
| public static function quadraticRecur(int $n): int | ||
| { | ||
| if ($n <= 0) { | ||
| return 0; | ||
| } | ||
| // 数组 nums 长度为 n, n-1, ..., 2, 1 | ||
| $nums = array_fill(0, $n, 0); | ||
| echo "递归 n = " . $n . " 中的 nums 长度 = " . count($nums) . "\n"; | ||
| return self::quadraticRecur($n - 1); | ||
| } | ||
|
|
||
| /* 指数阶(建立满二叉树)*/ | ||
| public static function buildTree(int $n) | ||
| { | ||
| if ($n == 0) { | ||
| return null; | ||
| } | ||
| $root = ['val' => 0, 'left' => null, 'right' => null]; | ||
| $root['left'] = self::buildTree($n - 1); | ||
| $root['right'] = self::buildTree($n - 1); | ||
| return $root; | ||
| } | ||
| } | ||
|
|
||
| /* Driver Code */ | ||
| $n = 5; | ||
| // 常数阶 | ||
| space_complexity::constant($n); | ||
| // 线性阶 | ||
| space_complexity::linear($n); | ||
| space_complexity::linearRecur($n); | ||
| // 平方阶 | ||
| space_complexity::quadratic($n); | ||
| space_complexity::quadraticRecur($n); | ||
| // 指数阶 | ||
| $root = space_complexity::buildTree($n); | ||
| echo "指数阶(建立满二叉树)完成\n"; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use CamelCase for class names in PHP. Same as all other class names.
Please carefully check the code and ensure that the code follows the coding standard of PHP.