Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions codes/php/chapter_computational_complexity/iteration.php
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";
85 changes: 85 additions & 0 deletions codes/php/chapter_computational_complexity/recursion.php
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 codes/php/chapter_computational_complexity/space_complexity.php
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
Copy link
Copy Markdown
Owner

@krahets krahets Nov 19, 2025

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.

{
/* 函数 */
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";
Loading