http://tenjj.qiniudn.com/to.jpg 目的是实现注释中的结果
1
lianyue 2015-04-01 17:21:49 +08:00 2
function trie($string) {
$ch = []; $yue = &$ch; foreach (str_split($string) as $value) { $ch[$value] = []; $ch = &$ch[$value]; } return $yue; } print_r(trie('apple')); |
2
582033 2015-04-01 17:24:08 +08:00 1
function trie($string, &$cu){
foreach (str_split($string) as $v) { $cu = &$cu[$v]; } return $cu; } $cu = []; trie('apple', $cu); print_r($cu); |
4
582033 2015-04-01 17:25:54 +08:00 1
foreach里你把变量覆盖了
|